-->
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.  [ 3 posts ] 
Author Message
 Post subject: How will I map my .hbm.xml file in this case and build HQL Q
PostPosted: Sun Mar 20, 2011 3:00 am 
Newbie

Joined: Sun Mar 20, 2011 2:36 am
Posts: 7
Hello friends,

I am new to Hibernet so pardon me for asking this basic question.


If we have two tables , TABLE A and TABLE B


Table A | Table B
----------------------------------------------------------------------------------
PK_ID COLA1 COLA2 COLA3 | PK_ID COLB1 COLB2 COLB3
A1 1 abc pqr | B1 1 abc pqr
A2 2 abc pqr | B2 1 abc pqr
A3 3 abc pqr | B3 1 abc pqr
A4 4 abc pqr | B4 1 abc pqr
----------------------------------------------------------------------------------

PK denotes Primary KEY of the Tables
/***************************/
if i want to write a query

select COLA1,COLB2,COLA3

from TABLE_A _a , TABLE_B _b

where
_a.PK_ID=_b.PK_ID
_a.COLA1=_b.COLB1 AND
_a.COLA2=_b.COLB2

/**************************/

[color=#FF0000][color=#FF0000][color=#FF0000]How will I design this in my Hibernet TABLE_A.hbm.xml and TABLE_B.hbm.xml file

How will I Write the HQL for this??
And WHAT WILL MY JAVA PERSISTENT CLASS LOOK LIKE.

Please Help me out as I am new to Hibernet and I need to do this as quick as Possible..
[/color][/color][/color]Lots Of Thanks in Advance..!!!!


Top
 Profile  
 
 Post subject: Re: How will I map my .hbm.xml file in this case and build HQL Q
PostPosted: Sun Mar 20, 2011 7:51 am 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
You really should go through a hibernate tutorial, if you do this is a simple thing to do..

<class
name="A"
table="A">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="COLA1" column="COLA1" />
//same for other properties
</class>

Then you need persistentclasses with name/variables with the same names as the name fields in the mapping, as well as setters and getters for the variables:

Class A {
int COLA1;
String COLA2;
//setters getters etc
}

HQL is pretty similar to SQL, i'm sure you could just translate the query piece by piece. But another way to do it:
//get session, begin transaction
List<A> allAs = session.createQuery("from A").list();
List<B> allBs = session.createQuery("from B").list();

List<A> result = new ArrayList<A>();
for(A a : allAs) {
for(B b : allBs) {
if(a.id == b.id && a.cola1 == b.colb1 && a.cola2 == b.colb2) {
result.add(a);
}
}
}
for (A a : result) {
System.out.println(a.cola1 + " " + a.cola2 + " " + a.cola3);
}


Top
 Profile  
 
 Post subject: Re: How will I map my .hbm.xml file in this case and build HQL Q
PostPosted: Tue Mar 22, 2011 2:29 pm 
Newbie

Joined: Sun Mar 20, 2011 2:36 am
Posts: 7
Thanks a lott for the reply.!!!!!


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