Hallo,
ich sitz gerade an nem ähnlichen Problem. Hab die m:n Beziehung mit einer Zwischentabelle und @IdClass gelöst. Ich hab:
Inventory <-n:1-> InventoryUser <-1:m-> User
Mein Problem ist jetzt aber, dass die Schlüssel von InventoryUser nicht die Werte der Schlüssel von Inventory und User annehmen, wenn ich neu in die Datenbank schreibe!
Code:
@Entity
public class Inventory {
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "INVENTORY_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private List<InventoryUser> inventoryUser = new ArrayList<InventoryUser>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "inventory")
public List<InventoryUser> getInventoryUser() {
return inventoryUser;
}
public void setInventoryUser(List<InventoryUser> productUser) {
this.inventoryUser = productUser;
}
// more attributes
Code:
@Entity
@Table(name = "User_Table")
public class User {
private String id;
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private Set<InventoryUser> inventoryUser = new LinkedHashSet<InventoryUser>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user")
public Set<InventoryUser> getInventoryUser() {
return inventoryUser;
}
public void setInventoryUser(Set<InventoryUser> inventoryUser) {
this.inventoryUser = inventoryUser;
}
// more attributes
and the intermediate entity
Code:
@Entity
@Table(name = "Inventory_User")
@IdClass(InventoryUser.ProductUserPK.class)
public class InventoryUser {
private Long inventoryId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getInventoryId() {
return inventoryId;
}
public void setInventoryId(Long inventoryId) {
this.inventoryId = inventoryId;
}
private String userId;
@Id
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
private User user;
@ManyToOne
@JoinColumn(name = "userId", insertable = false)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
private Inventory inventory;
@ManyToOne
@JoinColumn(name = "INVENTORY_ID", insertable = false)
public Inventory getInventory() {
return inventory;
}
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
private Date obtainDate = new Date();
@Basic
public Date getObtainDate() {
return obtainDate;
}
public void setObtainDate(Date obtainDate) {
this.obtainDate = obtainDate;
}
public static class ProductUserPK implements Serializable {
private Long inventoryId;
private String userId;
public Long getInventoryId() {
return inventoryId;
}
public void setInventoryId(Long inventoryId) {
this.inventoryId = inventoryId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public boolean equals(Object o) ...
@Override
public int hashCode() ...
}
}
So füge ich das ganze zusammen:
Code:
Inventory inventory = new Inventory();
User user = new User();
InventoryUser inventoryUser = new InventoryUser();
inventoryUser.setInventory(inventory);
inventoryUser.setUser(user);
inventory.getInventoryUser().add(inventoryUser);
user.getInventoryUser().add(inventoryUser);
und kriege dann so einen Fehler:
Code:
Could not execute JDBC batch update [insert into Inventory_User (obtainDate, INVENTORY_ID, userId) values (?, ?, ?)]
java.sql.BatchUpdateException: Column 'INVENTORY_ID' cannot be null