簡化堅持,在我的數據庫我有表:@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_fk`) REFERENCES `car` (`id_car`)
Addon (pk="id_addon")
不久:我有車,很多車可以有很多的插件(如ABS等)。
有汽車,插件和一個邏輯連接表。
總的來說,實體工作正常。當我想要保存單個對象時,我沒有存留數據的問題。我沒有問題,當我想要FETCH數據時,即。小車 - > 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();
在這種情況下,插件被存儲在車載表中的車載表中。 CarAddon表中沒有新的數據,儘管對象車具有良好的數據(在debbuger中有插件收集)。
當我改變em.persist(車)到em.merge(汽車)我有一個例外:
"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 ]."
簡易型我的類新:
@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
但這個DOS沒有解決我的問題。
ps2。我正在使用Netbeans 7,EclipseLink和MySQL(not Hibernate - I have problem with it)
從現在開始,你是我個人的上帝!完美的解決方法!!!!!!!! – vizzdoom