Hallo.
I have a problem with my ManyToMany mapping with an intermediate entity. My classes look like these:
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() ...
}
}
Now when i try to insert a new inventory - user relationship with following code:
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);
I get an error:
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
It seems that InventoryUser does not get the generated vaule of Inventory!
What am I doing wrong? Please help!