NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Cascading Primary Key value to an entity with a composite Primary Key

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
smallya



Joined: 25 Mar 2010
Posts: 9

PostPosted: Wed Nov 03, 2010 8:56 pm    Post subject: Cascading Primary Key value to an entity with a composite Primary Key Reply with quote

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)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo