-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate relation mappings
PostPosted: Thu Mar 13, 2008 9:48 am 
Newbie

Joined: Mon Dec 10, 2007 12:56 pm
Posts: 7
Hi all!!

I need some help with a very simple question, which I can't find an answer.
I have two very simple classes:

##Class CSyncId
Code:
public class CSyncId {
   private Long id;

   public CSyncId(){
      this.id = getNewSyncId();
   }
}

## Class CSyncData
Code:
public class CSyncData {

   private CSyncId syncId;
   private Long tmstmp;
   
   public CSyncData(String syncType){
      this.syncId = new CSyncId();
      tmstmp = 0L;
   }
}

I have a table T_SYNC_DATA, which maps CSyncData class.
The table T_SYNC_DATA has two NUMBER columns, called SYNC_ID (PK)and TMSTMP!!
Now I need a mapping between the class CSyncData and the table, but I don't know how make the relation beetween the column SYNC_ID and the property syncId in CSyncData, which is, in fact, an object of CSyncId.

I tried something like:

Code:
<hibernate-mapping package="slva.sync.data">
   <class name="CSyncData" table="T_SYNC_DATA">
      <one-to-one name="syncId" class="CSyncId"/>
      <property name="tmstmp" column="TMSTMP"/>
   </class>
   <class name="CSyncId">
      <property name="id" ???/>
   </class>
</hibernate-mapping>

I don't know how tell this mapping is a Primary Key, and how relation the 'id property of CSyncId (which id a Long)' with the column SYNC_ID.

Thanks a lot!!!!


Top
 Profile  
 
 Post subject: Re: Hibernate relation mappings
PostPosted: Thu Mar 13, 2008 11:09 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
It's a composite id.


Code:
<hibernate-mapping package="slva.sync.data">
   <class name="CSyncData" table="T_SYNC_DATA">
        <composite-id name="syncId" class="CSyncId">
                  <property name="id"  column="SYNC_ID"/>
         </composite-id>
      <property name="tmstmp" column="TMSTMP"/>
   </class>
</hibernate-mapping>




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 11:17 am 
Newbie

Joined: Mon Dec 10, 2007 12:56 pm
Posts: 7
Thanks a lot!!!

First, shouldn't be <key-property> instead of <property> in a <composite-id> tag?

Another question: What if I already have a composite key? One primary-key of them is a property of another class, like syncId. The other the tmstmp property inside the class mapped!!

Thanks, again!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 12:23 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
mamntc02 wrote:
Thanks a lot!!!

First, shouldn't be <key-property> instead of <property> in a <composite-id> tag?

Another question: What if I already have a composite key? One primary-key of them is a property of another class, like syncId. The other the tmstmp property inside the class mapped!!

Thanks, again!!



<key-property> is the right one. Mine is a bad copy paste ;) and for the second question why would you like to do that? May be you need <key-many-to-one..../>



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 4:24 am 
Newbie

Joined: Mon Dec 10, 2007 12:56 pm
Posts: 7
Yes, I guess so.

In first table I just need the 'syncId' as primary key. And your example works perfectly.
However, I have another table with a composite PK with two columns. First syncId, as previous. The other is a String Java attribute indicating an state. In this table syncId is not unique, 'cause the same Id may have several states. This is the Java associated class:
Code:
public class CSyncProcess implements Serializable{
   private final CSyncId syncId;
   private String syncState;
}

I think I already solved, however I haven't tested yet, 'cause I haven't finish the whole program. I made that relation as follows:
Code:
<hibernate-mapping package="slva.sync.data">
   <class name="CSyncData" table="T_SYNC_DATA">
        <composite-id>
         <key-many-to-one name="syncId" column="SYNC_ID" class="CSyncId"/>
         <key-property name="syncState" column="SYNC_STATE"/>
         </composite-id>
    </class>
</hibernate-mapping>

Should it work?

Thanks!!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 5:29 am 
Newbie

Joined: Mon Dec 10, 2007 12:56 pm
Posts: 7
Well, I'm afraid it doesn't work!!!

Hibernate tells me:
Quote:
Caused by: org.hibernate.MappingException: An association from the table T_SYNC_PROCESS refers to an unmapped class: slva.sync.data.CSyncId

How can I fix that? I don't know whether CSyncId must be declared as a mapping, however that class has no relation table. It's just to create ids!!!!
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 9:35 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Umm, what I understand from your explanation is your an entity A that can have multiple states. In this case I would say you need to do A as you did and you need to define a collection of elements for it, which keeps the states. You can also get rid of the key class since your key is a simple property.


Code:
   <class name="CSyncData" table="T_SYNC_DATA">
        <id name="id" column="SYNC_ID"/>
      <property name="tmstmp" column="TMSTMP"/>

<set name="states" table="THE_STATES_TABLE">
    <key column="CSyncData_ID"/>
    <element type="string" column="SYNC_STATE"/>
</set>

   </class>
</hibernate-mapping>



This is a quite hypothetical example and you need to modify column and table names accordingly.


Farzad-


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