smallya
Joined: 25 Mar 2010 Posts: 9
|
Posted: Wed Nov 03, 2010 8:56 pm Post subject: Cascading Primary Key value to an entity with a composite Primary Key |
|
|
My use case is a little more complex (although methinks it is a very common case) so bear with my explanation. Here is the lay of the land.
I have two three entities - Product, UserList, ListContent. The structure of the entities and then at the bottom I have explained what is happening.
Product
@Entity
@Table(name="products")
-----------------
@Id
@Basic(optional = false)
@Column (name="product_code")
private String productCode;
List
@Entity
@Table(name="user_lists")
----------------
@Id
@SequenceGenerator(name="User_Lists_Id_Gen", sequenceName="user_lists_list_id_seq", allocationSize=1)
@GeneratedValue(generator="User_Lists_Id_Gen")
@Basic(optional = false)
@Column(name = "list_id")
private Integer listId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userList")
private List<ListContent> listContentList;
public List<ListContent> getListContentList() {
return listContentList;
}
public void setListContentList(List<ListContent> listContentList) {
this.listContentList = listContentList;
}
ListContent
@Entity
@Table(name="list_content")
------------------
@EmbeddedId
protected ListContentPK listContentPK;
@JoinColumn(name = "list_id", referencedColumnName = "list_id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private UserList userList;
@JoinColumn(name = "product_code", referencedColumnName = "product_code", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Product product;
ListContentPK
@Embeddable
---------------------------
@Basic(optional = false)
@Column(name = "list_id")
private Integer listId;
@Basic(optional = false)
@Column(name = "product_code")
private String productCode;
1. User adds one or more items to a new list.
2. A ListContent object is created with product_code, unit, quantity etc and added to the List using setListContentList()
3. Note: List ID is not yet generated since we don't know if user will save or discard.
4. Note: Even though ListContent has a composite PK and we don't have the list_id yet we are creating rows - productCode is already part of the row now.
4. When user clicks save
- a persist() is called on List entity - which also generates LIST_ID
- since CASCADEALL has been set on LIST-LISTCONTENT relationship - the ID should be propogated to LISTCONTENT.
5. But it seems like the listID is not getting cascaded down and I am getting a Database Exception
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "list_id" violates not-null constraint
Error Code: 0
Call: INSERT INTO list_content (created_by, uom, modified_by, quantity, created_date, cost, modified_date, product_code, list_id, store_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [4, LB, 4, 1, 2010-11-03 13:04:40.266, null, 2010-11-03 13:04:40.266, ASPARAGUS, null, null]
Query: InsertObjectQuery(com.meera.application.entity.ListContent[listContentPK=com.meera.application.entity.ListContentPK[listId=null, productCode=ASPARAGUS]])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333) |
|