-->
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.  [ 2 posts ] 
Author Message
 Post subject: Persisting child objects with Composite key
PostPosted: Thu May 03, 2007 2:08 am 
Newbie

Joined: Tue May 01, 2007 5:48 pm
Posts: 6
Hi All,

I have a parent table - ENDPOINT. This table has a primary key (ENDPOINT_ID) which is just a one-up-number. There are some other fields for this table as well.
There is a child table - SYNONYM which has only 2 fields - Foreign key (ENDPOINT_ID from ENPOINT) and another field called as SYNONYM_TEXT. The primary key for this table has been defined as a composite key comprising both these fields mentioned above.

Association: Parent can have zero or more child objects. In other words a Parent object can have a Set of child objects.

Now the requirement is very simple - A user is going to enter details for parent and child fields on the UI. Taking them as input an Object of ENPOINT (parent class) and a set of SYNONYM objects are created. On persisting the parent the children should automatically be persisted to the child table.

For the above to happen we have done the following:

1. Created a mapping file for the parent table which is attached with the mail (Enpoint.hbm.xml). Here we have defined the one-to-many relationship between the parent and the child table.
2. Created a mapping file for the child table. Here the composite key is defined and a many-to-one mapping to the parent is defined. Each of the child objects will have an instance of the parent object. Since there is a composite key in the child table equals and hashCode methods have been overridden.
3. Save is called on the parent using Hibernate Spring template.

The following is the result:
It fires the INSERT query to the parent table and properly inserts the values into the Parent table. It then fires only an UPDATE query on the child table. There is not INSERT query fired on the child table and eventually the child table is never populated with values.

If you know what is the problem then please indicate the same else it will be great if you could get some help on the same.

Thanks,
madhav


Code:
<hibernate-mapping package="com.pfizer.pgrd.like.endpoint.model">
   <class name="EndpointSynonym" table="ENDPOINT_SYNONYM">
      <composite-id>
         <key-property name="epID" type="long" column="ENDPOINT_ID"/>
         <key-property name="synonymText" type="string" column="SYNONYM_TEXT"/>
      </composite-id>
       <many-to-one
         name="endpoint"
         class="Endpoint"
         column="ENDPOINT_ID"
         update="false"
         insert="false"
         not-null="true"/>
   </class>
</hibernate-mapping>

<hibernate-mapping package="com.pfizer.pgrd.like.endpoint.model">
   <class name="Endpoint" table="ENDPOINT">
      <!-- Primary key  -->
      <id name="epID" unsaved-value="null" type="long">
         <column name="ENDPOINT_ID" not-null="true"/>
         <generator class="increment"/>
       </id>
      <property name="epName" type="string" column="ENDPOINT_NAME" not-null="true"/>
      <property name="epAcronym" type="string" column="ENDPOINT_ACRONYM"/>
       <property name="epDefinition" type="string" column="ENDPOINT_DEFINITION"/>
       <property name="epAnalysisReferences" type="string" column="ENDPOINT_ANALYSIS_REFERENCES"/>
       <property name="epVersionNumber" type="long" column="VERSION_NUMBER" not-null="true"/>
       <property name="epLastUpdatedBy" type="string" column="LAST_UPDATED_BY"/>
       <property name="epCreatedBy" type="string" column="CREATED_BY" not-null="true"/>
       <property name="epWhenCreated" type="timestamp" column="WHEN_CREATED" not-null="true"/>
       <property name="epWhenLastUpdated" type="timestamp" column="WHEN_LAST_UPDATED"/>
      <!-- Association: Synonym -->
      <set name="epSynonyms" inverse="true" table="ENDPOINT_SYNONYM" cascade="all">
         <key column="ENDPOINT_ID"/>
         <one-to-many class="EndpointSynonym"/>
      </set>
   </class>
</hibernate-mapping>


public class Endpoint implements Serializable{

   private static final long serialVersionUID = 1L;

   /** identifier field */
   private long epID;
   /** non-nullable persistent field */
   private String epName;
   /** nullable persistent field */
   private String epAcronym;
   /** nullable persistent field */
   private String epDefinition;
   /** nullable persistent field */
   private String epAnalysisReferences;
   /** non-nullable persistent field */
   private long epVersionNumber;
   /** nullable persistent field */
   private String epLastUpdatedBy;
   /** non-nullable persistent field */
   private String epCreatedBy;
   /** non-nullable persistent field */
   private Date epWhenCreated;
   /** nullable persistent field */
   private Date epWhenLastUpdated;
   private Set epSynonyms = new HashSet();   
   //private EndpointSynonym epSynonym;
   
   /** Default constructor */
   public Endpoint(){
   }
   
   public void setEpID(long id){
      
      this.epID = id;
   }

   public long getEpID(){
      return epID;
   }
   public String getEpName() {
      return epName;
   }
   public String getEpAcronym() {
      return epAcronym;
   }
   public String getEpDefinition() {
      return epDefinition;
   }
   public String getEpAnalysisReferences() {
      return epAnalysisReferences;
   }
   public long getEpVersionNumber() {
      return epVersionNumber;
   }
   public String getEpLastUpdatedBy(){
      return epLastUpdatedBy;
   }
   public String getEpCreatedBy(){
      return epCreatedBy;
   }
   public Date getEpWhenCreated(){
      return epWhenCreated;
   }
   public Date getEpWhenLastUpdated(){
      return epWhenLastUpdated;
   }
   public Set getEpSynonyms() {
      return epSynonyms;
   }

   
   public void setEpName(String name) {
      this.epName = name;
   }
   public void setEpAcronym(String acronym) {
      this.epAcronym=acronym;
   }
   public void setEpDefinition(String definition) {
      this.epDefinition = definition;
   }
   public void setEpAnalysisReferences(String analysisReferences) {
      this.epAnalysisReferences = analysisReferences;
   }
   public void setEpVersionNumber(long versionnumber) {
      this.epVersionNumber=versionnumber;
   }
   public void setEpLastUpdatedBy(String lastUpdatedBy) {
      this.epLastUpdatedBy= lastUpdatedBy;
   }
   public void setEpCreatedBy(String createdBy) {
      this.epCreatedBy= createdBy;
   }
   public void setEpWhenCreated(Date whenCreated) {
      this.epWhenCreated = whenCreated;
   }   
   public void setEpWhenLastUpdated(Date whenLastUpdated) {
      this.epWhenLastUpdated = whenLastUpdated;
   }
   public void setEpSynonyms(Set synonyms) {
      this.epSynonyms = synonyms;
   }
   public void addEpSynonym(EndpointSynonym epSynonym){
      this.epSynonyms.add(epSynonym);
      epSynonym.setEndpoint(this);
   }
}

public class EndpointSynonym implements Serializable{
   private static final long serialVersionUID = 1L;

   /** identifier field */
   private long epID;
   /**nullable persistent field */
   private String synonymText;
   private Endpoint endpoint;
   
   public void setEpID(long id){
      this.epID=id;
   }
   public long getEpID(){
      return epID;
   }
   public String getSynonymText(){
      return this.synonymText;
   }
   public void setSynonymText(String synonym){
      this.synonymText=synonym;
   }
   public Endpoint getEndpoint(){
      return endpoint;
   }   
   public void setEndpoint(Endpoint endpoint){
      this.endpoint=endpoint;
   }   
   /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   public int hashCode() {
      final int PRIME = 31;
      int result = 1;
      result = PRIME * result + (int) (epID ^ (epID >>> 32));
      result = PRIME * result + ((synonymText == null) ? 0 : synonymText.hashCode());
      return result;
   }
   /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final EndpointSynonym other = (EndpointSynonym) obj;
      if (epID != other.epID)
         return false;
      if (synonymText == null) {
         if (other.synonymText != null)
            return false;
      } else if (!synonymText.equals(other.synonymText))
         return false;
      return true;
   }



Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 10:59 am 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
It appears that your child table/class SYNONYM, declares it's primary id as a "long". It doesn't declare an unsaved-value.

Could it be that Hibernate doesn't know whether it's an update or an insert.

Either declare your local IDs as "Long", or declare an unsaved value in your mapping file.

_________________
Please rate this reply if it helps!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.