-->
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.  [ 13 posts ] 
Author Message
 Post subject: Help needed in mapping Mutiple one-to-one
PostPosted: Mon Feb 23, 2009 1:45 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
Hi All,
table A,B,C,d1,d2,d3,E,F
E-->A many:1
A --> B 1:1
B-->C 1:1
C-->d1 1:1
C-->d2 1:1
C-->d3 1:1
F-->C many:1


Please let me know how can I setup such a relation in .hbm.xml files to insert update delete & select.

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 1:52 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
You can map these classes like any other class. There is no problem in one class having multiple relations. Plz give any specific doubts if you have any.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 2:31 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
can you please show me a sample mapping for table C?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 2:59 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I am assuming that ur 1-1 relations are using foreign keys with unique constraint.

Code:
    <class name="C" table="C">
       <id name="id" column="ID">
               <generator class="native"/>
       </id>
      
       <many-to-one name="d1" class="D1" column="D1_ID" unique="true"/>
       <many-to-one name="d2" class="D2" column="D2_ID" unique="true"/>
       <many-to-one name="d3" class="D3" column="D3_ID" unique="true"/>
      
       <one-to-one name="b" class="B" property-ref="c"/>
      
       <set name="fs" table="F">
           <key column="F_ID" />
           <one-to-many class="F"/>
         </set>
    </class>
   
In Mapping for B u ll have:

    <class name="B" table="B">
       ...........
       ...........
       <many-to-one name="c" class="C" column="C_ID" unique="true"/>
       ...........
       ...........
    </class>
   
In Mapping for D1, D2, D3 u ll have:
    <class name="D1" table="D1">
       ...........
       ...........
       <one-to-one name="c" class="C" property-ref="d1"/>
       ...........
       ...........
    </class>

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 3:04 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
hi thx u for the reply ..but I dont want to create 3 seperate objects d1,d2,d3 in the C class.Can I not use another technique like subclass.
since One record in C wil always point to only one record of either of d1,d2,d3


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 3:13 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Sure you can use, if in ur domain model d1, d2 d3 are subclasses of C then you should be using the sub-class construct than the one-to-one relation. So in that case instead of the unique foreign key, you should be using the primary key association technique.

So the subclass D1 will look like:
Code:
   <class name="C" table="C">
         ...........
         ...........
      <joined-subclass name="D1" table="D1">
         <key column="C_ID" />
         <property name="col1" column="COL1" />
         ...........
         ...........
      </joined-subclass>
   </class>


Now your table D1 will have primary key column C_ID referencing C.ID

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 3:51 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
In Mapping for B u ll have:

<class name="B" table="B">
...........
...........
<many-to-one name="c" class="C" column="C_ID" unique="true"/>
...........
...........
</class>


why there should be a many-to-one mappig when the relationship is 1-to-1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 3:59 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
This is how we map 1-1 with unique foreign key constraint. You can see that there is a unique=true attribute and this will distinguish the 1-1 from n-1.

So <many-to-one unique="true"/> is the original side of the relation (table has a unique foreign key column) and
<one-to-one property-ref="c"/> is the inverse of that relation.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:40 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
I tried doing below entry
in my class B
Underlying is my class C;
as per the Datamodel underlyingName (from Underling table) is a foreign key to my table B.
<many-to-one name="underlying" class="com.agdelta.trade.marketdata.model.Underlying" lazy="false"
column="underlyingName" unique="true"/>


throws below exception

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.agdelta.trade.marketdata.model.Underlying
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3128)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:479)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:204)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:127)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.agdelta.trade.marketdata.dao.QuoteDAO.addNewQuote(QuoteDAO.java:32)
at com.agdelta.trade.marketdata.QuoteTest.testQuote(QuoteTest.java:30)
at com.agdelta.trade.TradeStartupServlet.init(TradeStartupServlet.java:48)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3956)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4230)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3025)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:432)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1278)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1570)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1579)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1579)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1559)
at java.lang.Thread.run(Unknown Source)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:52 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
I got the answer for the same.Actually I was trying to set the value in d1 from Class B ,where as I should set the value in C.
Thanks a lot.You have really helped me to get the things clear.
cheers.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 11:00 pm 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
Hi,
I am getting one more exception.Class Cast Exception.
Following are the details
table C mapped to d1,d2,d3 using subclass
E --> C many : many
but the E to C is via a mapping table between them which contains only the primary keys of table E & C.
While I try to persist to database I set the object of "C" into the object of "E" and then save the Object E.
But this throws me a classcast exception.

Mapping in class E

<class name="E" table="ee" >
<id name="eId" type="int">
<column name="eId" />
<generator class="assigned" />
</id>
.
.
.
<set name="cData" table="cematrix" lazy="false" cascade="all">
<key column="e"/>
<many-to-many column="cName" class="C"/>
</set>

</class>
</hibernate-mapping>


Mapping in class C

<hibernate-mapping>
<class name="C" table="c" >
<id name="cName" type="string">
<column name="cName" />
<generator class="assigned" />
</id>
.
.
.
<joined-subclass name="D1" table="dd1">
<key column="d1" update="true"></key>

.
.
.
</joined-subclass>


<joined-subclass name="D2" table="dd2">
<key column="d2" update="true"></key>

.
.
.
</joined-subclass>

<joined-subclass name="D3" table="dd3">
<key column="d3" update="true"></key>

.
.
.
</joined-subclass>

</class>
</hibernate-mapping>


Code for persist the object

E spc = new E(1);
spc.set..("AC1");
spc.set..("BDC1");
spc.set..("C1");
spc.setEID(1);
spc.set..("COT");
spc.set..("I1");

C u = new C("U1");
spc.setC(u);
eDAO dao = new eDAO();
dao.addNewE(spc);


This throws Class Cast Exception

java.lang.ClassCastException: com..trade.md.model.C
at org.hibernate.type.SetType.wrap(SetType.java:39)
at org.hibernate.event.def.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:84)
at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:51)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:98)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:371)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:273)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 2:00 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Why are you doing this?
Code:
C u = new C("U1");
spc.setC(u);

Your E->C is MTM then you should be setting a collection of C to E not an object of C. So you should do something like:
Code:
C u = new C("U1");
Set<C> cs=new HashSet<C>();
cs.add(u);
spc.setC(cs);

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 2:04 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
yes u are right.Its working now.
thank you.


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