java – @ManyToMany – 数据不会在数据库中持久存在

简化,在我的数据库中我有表:Car (pk=id_car)CarAddon (pk=id_car_fk,id_addon_fk, `FK_car_addon_addon` FOREIGN KEY (`id_addon_fk`) REFERENCES `addon` (`id_addon`)`FK_car_addon_car` FOREIGN KEY (`id_...

简化,在我的数据库中我有表:

Car (pk="id_car")

CarAddon (pk="id_car_fk,id_addon_fk", 
`FK_car_addon_addon` FOREIGN KEY (`id_addon_fk`) REFERENCES `addon` (`id_addon`)
`FK_car_addon_car` FOREIGN KEY (`id_car_fk`) REFERENCES `car` (`id_car`)

Addon (pk="id_addon")

简而言之:我有车,很多车都有很多插件(比如ABS等).
有桌子有汽车,插件和一个表是逻辑连接.

总体而言,实体运作良好.当我想要持久化单个对象时,我对持久数据没有任何问题.当我想要FETCH数据时,我没有问题,即.小车 – &GT getAddon();

但是,当我要坚持收藏时,没有任何反应.没有抛出异常,数据库中没有新数据.

//DBManager is a singleton to create an EntityManager
EntityManager em = DBManager.getManager().createEntityManager();

em.getTransaction().begin();
Addon addon1 = new Addon();
addon1.setName("czesc1");
em.persist(addon1);
Addon addon2 = new Addon();
addon2.setName("czesc2");
em.persist(addon2);

car.setAddonCollection(new ArrayList<Addon>());
car.getAddonCollection().add(addon1);
car.getAddonCollection().add(addon2);
em.persist(car);
em.getTransaction().commit();

在这种情况下,插件存储在Addon表中,car存放在Car表中.虽然对象车具有良好的数据(在debbuger中有插件集合),但CarAddon表中没有新数据.

当我将em.persist(car)改为em.merge(car)时,我得到了一个例外:

"SEVERE: Persistence error in /admin/AddAuction : java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: model.entity.Car[ idCar=0 ]."

我的classess的简单版本:

  @Entity
  @Table(name = "addon")
  @XmlRootElement
  @NamedQueries({...})
  public class Addon implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @NotNull
      @Column(name = "id_addon")
      private Integer idAddon;

      @Size(max = 100)
      @Column(name = "name")
      private String name;

      @JoinTable(name = "car_addon", 
          joinColumns = {
          @JoinColumn(name = "id_addon_fk", referencedColumnName = "id_addon")}, 
          inverseJoinColumns = {
          @JoinColumn(name = "id_car_fk", referencedColumnName = "id_car")})
      @ManyToMany
      private List<Car> carCollection;

      @XmlTransient
      public List<Car> getCarCollection() {
          return carCollection;
      }

      public void setCarCollection(List<Car> carCollection) {
          this.carCollection = carCollection;
      }
  }



@Entity
@Table(name = "car")
@XmlRootElement
@NamedQueries({...)
public class Car implements Serializable {

    @ManyToMany(mappedBy = "carCollection", fetch= FetchType.EAGER, cascade=CascadeType.ALL)
    private List<Addon> addonCollection;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id_car")
    private Integer idCar;

    @XmlTransient
    public List<Addon> getAddonCollection() {
        return addonCollection;
    }

    public void setAddonCollection(List<Addon> addonCollection) {
        this.addonCollection = addonCollection;
    }
}

我该如何解决?

PS1.我有:

cascade=CascadeType.ALL przy @ManyToMany private List<Car> carCollection

但这不能解决我的问题.

PS2.我正在使用Netbeans 7,EclipseLink和MySQL(not Hibernate – I have problem with it)

解决方法:

我有一个理论似乎总是带着多对多的集合来绊倒人们.问题是在内存中,关联是在两个地方进行的.在汽车的插件列表和插件的汽车列表中都有.在数据库中,没有这样的重复.

JPA提供者解决这个问题的方式是通过mappedBy属性.由于你已经在汽车的插件列表中进行了映射,这意味着该关系实际上是由插件的汽车列表控制的(我知道这很混乱).

尝试添加以下内容:

addon1.setCarCollection(new ArrayList<Car>());
addon1.getCarCollection().add(car);

addon2.setCarCollection(new ArrayList<Car>());
addon2.getCarCollection().add(car);

在你坚持下去之前.

本文标题为:java – @ManyToMany – 数据不会在数据库中持久存在

基础教程推荐