-->
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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate mapping
PostPosted: Wed Jan 28, 2009 4:07 pm 
Newbie

Joined: Fri Dec 12, 2008 8:59 pm
Posts: 10
Hi,

I need help on hibernate mapping, am too confused on this, plz suggest me the approach.

i have table and A, B and C

A is parent table, and B, C are child objects.

There is id coloumn in primary key of table A refered to foreign key to the B and C,

suppose if am searching based on some field, it should retrive the vale from the table B and C. mostly all columns will be same in both table B and C.

query like

select * from B where type="q" union select * from C where type="q"

i have confused on using join-subclass or some other, could you plz tell me wat are the ways we can do this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 5:18 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
If ClassA has properties for ClassB and ClassC, you may use a many-to-one (if single instance):
<many-to-one name="itemB" column="ID" class="ClassB"/>
<many-to-one name="itemC" column="ID" class="ClassC"/>

or for collection
<class name="ClassA" table="TABLEA">
...
<set name="itemB">
<key column="ID"/>
<one-to-many class="ClassB"/>
</set>
<set name="itemC">
<key column="ID"/>
<one-to-many class="ClassC"/>
</set>
</class>


If you want a union, you may write a loader query:
<class name="ClassA" table="TABLEA">
<id name="id" column="ID">
<loader query-ref="loadClassBC"/>
...
</class>

<sql-query name="loadClassBC">
<return alias="bc" class="ClassBC"/>
select b.ID as {bc.id},
...
from TABLEB b
where b.ID = ?
union
select c.ID as {bc.id},
...
from TABLEB c
where c.ID = ?
</sql-query>


---
please rate


Top
 Profile  
 
 Post subject: Hibernate mapping
PostPosted: Wed Jan 28, 2009 6:55 pm 
Newbie

Joined: Fri Dec 12, 2008 8:59 pm
Posts: 10
Thanks a lot lin, i have one more doubt,

<set name="itemB">
<key column="ID"/>
<one-to-many class="ClassB"/>
</set>
<set name="itemC">
<key column="ID"/>
<one-to-many class="ClassC"/>
</set>
</class>

in that <one-to-many class="ClassB"/> defines one record in class A, but B have many records rite?

and i am searching colum with present in table b and its not in table A, will it give the search results or we can able to search only column present in the table A?

plz correct me,if am wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 5:45 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
Here we are mapping ClassA. ClassA contains a set of ClassB called itemB (may be we should call it itemBs as it is a collection). You would need to map ClassB also.
Now, if you load ClassA, it would select data from ClassB and populate the set itemBs. Is this what you are trying to achieve?

---
please rate


Top
 Profile  
 
 Post subject: Hibernate mapping
PostPosted: Fri Jan 30, 2009 7:15 pm 
Newbie

Joined: Fri Dec 12, 2008 8:59 pm
Posts: 10
Hi Lin,

Acutally my requirment is,

i need to search from the table b and table c, table a is parent, but its contain only few columns of the child table b anc table c.

so if am search based on columns like name, it should retrieve the records from b and c its like union.

this is my requirment, so how can we create serialise object, so the parent contain all the child columns fields?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 6:58 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
There's no union in HQL but you could do something like
Code:
FROM tableA a
LEFT JOIN a.Bees b
LEFT JOIN a.Cees c
WHERE
b.value = ?
or c.value = ?


Top
 Profile  
 
 Post subject: Hibernate collections
PostPosted: Sat Jan 31, 2009 12:21 pm 
Newbie

Joined: Wed Jan 14, 2009 11:20 pm
Posts: 3
hi

this is another way to approach with collections

here is a complete sample on how to join tables. You can download and explore the source code.
http://forum.hibernate.org/viewtopic.php?t=994020&highlight=hibernate+sample


Top
 Profile  
 
 Post subject: Hibernate mapping
PostPosted: Tue Feb 03, 2009 6:31 pm 
Newbie

Joined: Fri Dec 12, 2008 8:59 pm
Posts: 10
Thanks lin and msantos. i will try with these options..


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