-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Autogenerated Composite Keys and Null issues
PostPosted: Tue Mar 07, 2006 3:30 pm 
Newbie

Joined: Mon Nov 21, 2005 5:21 pm
Posts: 6
I can see this issue being opened many times in the forum but with no definite resolution.

Is it true that unique identifer generator cannot be used to automatically save child relationship when I save parent?

If No, Here is my scenario.

I have a class called Partner ( PK partnerID) and Content( PK ContentID).
Also a linking table called Partner_Content ( composite PK of PartnerID and ContentID represented by PartnerContentPK class).


Now I create Partner instance, call all the setters. ALso create PartnerContentPK and call its setters( although partnerID is still not generated at this point) . I add PartnerContentPK to Partner through
add method in Partner to add partnerContact.
public void addPartnerContent(PartnerContent pc) {
pc.setPartner(this);
this.getPartnerContents().add(pc);
}

When I save Partner, it tries to save PartnerContact first but because Hibernate cannot read just created partnerID, it simply tries to insert null in the partnerID. Because of the DB restriction, I am getting "cannot insert null" SQL Error.

Isn't Hibernate smart enough to pick the PartnerId or do I have to tweak something else to make it work?

Thanks in advance

Thapak


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 9:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Do you have inverse="true" in your Partner class' <set> mapping? If you do, remove it, that'll be your problem. If you don't, can you post your full mapping (in code tags) so we can investigate?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 2:05 pm 
Newbie

Joined: Mon Nov 21, 2005 5:21 pm
Posts: 6
Unfortunately, removing inverse="true" does not help.
Here is my PartnerContent.hbm.xml

<hibernate-mapping>
<class
name="xxx.partner.model.PartnerContent"
table="PARTNER_CONTENT"
>

<composite-id name="comp_id" class="xxx.partner.model.PartnerContentPK">
<key-property
name="partnerId"
column="PARTNERID"
type="java.lang.Long"
length="10"
/>
<key-property
name="contentId"
column="CONTENT_ID"
type="java.lang.Long"
length="10"
/>
</composite-id>

...

Partner.hbm.xml has mapping with partnercontent as below:

<set
name="partnerContents"
lazy="true"
cascade="all-delete-orphan"
>
<key>
<column name="PARTNERID" />
</key>
<one-to-many
class="xxx.partner.model.PartnerContent"
/>
</set>

Partner.java has

public void addPartnerContent(PartnerContent partnerContent) {
partnerContent.setPartner(this);
this.getPartnerContents().add(partnerContents);
}

populatePartnerwithContent() {
PartnerContent pcBean = PartnerContent();
PartnerContentPK pcPK = new PartnerContentPK();
pc.setPartnerId(this.getPartnerId());
pc.setContentId(smallLogoContent.getContentId());
pcBean.setComp_id(partnerContentPK);
pcBean.setContent(contentBean);
this.addPartnerContent(pcBean);
}

public class PartnerContentPK implements Serializable {

/** identifier field */
private Long partnerId;

/** identifier field */
private Long contentId;

/** full constructor */
public PartnerContentPK(Long partnerId, Long contentId) {
this.partnerId = partnerId;
this.contentId = contentId;
}

/** default constructor */
public PartnerContentPK() {
}

public Long getPartnerId() {
return this.partnerId;
}

public void setPartnerId(Long partnerId) {
this.partnerId = partnerId;
}

public Long getContentId() {
return this.contentId;
}

public void setContentId(Long contentId) {
this.contentId = contentId;
}

public String toString() {
return new ToStringBuilder(this)
.append("partnerId", getPartnerId())
.append("contentId", getContentId())
.toString();
}

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( !(other instanceof PartnerContentPK) ) return false;
PartnerContentPK castOther = (PartnerContentPK) other;
return new EqualsBuilder()
.append(this.getPartnerId(), castOther.getPartnerId())
.append(this.getContentId(), castOther.getContentId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getPartnerId())
.append(getContentId())
.toHashCode();
}

}

/** @author Hibernate CodeGenerator */
public class PartnerContent implements Serializable {

/** identifier field */
private PartnerContentPK comp_id;

/** persistent field */
private Content content;

/** nullable persistent field */
private Partner partner;

...constructors and getters/setters

}


Exception looks like:
08 Mar 2006 11:40:54,189 ERROR (org.hibernate.util.JDBCExceptionReporter:58) - ORA-01400: cannot insert NULL into ("CUADMIN"."PARTNER_CONTENT"."PARTNERID")

08 Mar 2006 11:40:54,199 ERROR (org.hibernate.event.def.AbstractFlushingEventListener:277) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:72)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:67)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:148)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1848)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2209)
.....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 5:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm afraid I can't help you with this one. I can't figure out what's going on with the composite id. If the composite id is made up of ids of hibernate-mapped objects, why are you dealing with the ids yourself? Shouldn't the compsite key be key-many-to-ones, not key-propertys? How does hibernate figure out that the key-property in PartnerContent is the id of Partner? You've mapped it as a property unrelated to anything else. I admit to not having used composite keys too often, and never in the manner you're using them, so I can't tell if what you're doing is feasible. Sorry.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.