-->
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.  [ 10 posts ] 
Author Message
 Post subject: Regarding Hibernate Query
PostPosted: Mon Feb 13, 2006 1:40 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
Hello Experts,

Thanks for Reading and Solving this Problem.

My question is:
for eg. get all the records from single Table:

hibernate query is : from <table name>

that's fine.

How can I connect two tables and get the results. Pls give me some idea.

Thanks

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:34 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

read this,

http://www.hibernate.org/hib_docs/v3/re ... rycriteria

List cats = sess.createCriteria(Cat.class)
.createAlias("kittens", "kt")
.createAlias("mate", "mt")
.add( Restrictions.eqProperty("kt.name", "mt.name") )
.list();

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 12:43 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
To do the same thing using a query instead of Criteria, use
Code:
from Table t
join t.RelatedEntity r
Of course, there must be a relationship called RelatedEntity in your mapping file (relationshsip = any of one-to-one, one-to-many, many-to-one, many-to-many, set, map, list, bag, idbag.. and probably a few others that I've forgotten).

If you want to join two tables that aren't related in mapping (e.g. their mappings include ids, not entities) you could use SQL-like syntax:
Code:
select a from TableA a, TableB b
where a.refId = b.refId


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 8:25 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
for eg. I have a two table.

1. DEPTS
2. EMPLOYEE

BOTH FILES LOOK LIKE
**********************************************************
DEPTS.HBM.XML
**********************************************************
<class name="Depts" table="DEPTS">
<id name="deptno" column="DEPTNO" type="short">
<generator class="assigned"/>
</id>

<property name="deptname" column="DEPTNAME" type="string" />
<property name="location" column="LOCATION" type="string" />

<set name="employeeSet" inverse="true">
<key column="DEPTNO"/>
<one-to-many class="Employee"/>
</set>
</class>

**********************************************************
EMPLOYEE.HBM.XML
**********************************************************
<class name="Employee" table="EMPLOYEE">
<id name="empno" column="EMPNO" type="short">
<generator class="assigned"/>
</id>

<property name="empname" column="EMPNAME" type="string" />
<property name="salary" column="SALARY" type="float" />

<many-to-one name="depts" column="DEPTNO" class="Depts" />
</class>
**********************************************************

I am using oracle 9i
for ordinary query

select * from employee e, depts d where d.deptno=e.deptno is working.
but how to write in Hibernate?

Pls reply this. Thanks

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 5:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Quote:
select * from employee e, depts d where d.deptno=e.deptno is working

This selects all Employees, along with their departments. There are many ways to do this in Hiberante.
Code:
from Depts
This selects all departments. When you access the employeeSet in each Dept object, the relevant employees will be loaded.
Code:
select d, e from Dept d join Employee e
This returns a list/iterator of Object[2]s: the Object[1] will be an Employee, and Object[0] will be the Dept that the Employee is in. This is a little over the top, because the employeeSet in each Dept will still fetch Employees, but it does make certain loops a little more readable.
Code:
from Employee
This selects all employees. Employee.getDepts() will return the Dept that the employee is in.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 16, 2006 4:43 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
Hi,

StringBuffer query = new StringBuffer(" from Employee as E,Depts as D Where D.deptno=E.depts");
String strQuery = query.toString();
Query q = session.createQuery( strQuery );
System.out.println(q.toString());
List students = q.list();

The above query is working fine. i got a correct list.size();

But how to get all the fields of two tables.

i wrote like this

Iterator itrStud = students.iterator();
for( int index = 0 ; itrStud.hasNext() ; index++){
Object s1 = (Object) itrStud.next();
System.out.println("values :"+s1);
}

but i got values like

values: [Ljava.lang.Object;@1a116c9
values: from(depts)[Ljava.lang.Object;@1d1e730

How can i get all this fields?

Thanks

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 16, 2006 4:40 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
To quote my earlier post:
Quote:
This returns a list/iterator of Object[2]s: the Object[1] will be an Employee, and Object[0] will be the Dept that the Employee is in. This is a little over the top, because the employeeSet in each Dept will still fetch Employees, but it does make certain loops a little more readable.

Though I wrote my query the other way around, so for the way you wrote your query, the employee will be Object[0] and the dept will be Obect[1].


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 1:01 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
tenwit wrote:
To quote my earlier post:
Quote:
This returns a list/iterator of Object[2]s: the Object[1] will be an Employee, and Object[0] will be the Dept that the Employee is in. This is a little over the top, because the employeeSet in each Dept will still fetch Employees, but it does make certain loops a little more readable.

Though I wrote my query the other way around, so for the way you wrote your query, the employee will be Object[0] and the dept will be Obect[1].


please tell me clearly, Could u send me how to get those two object in hibernate.

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 1:01 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
tenwit wrote:
To quote my earlier post:
Quote:
This returns a list/iterator of Object[2]s: the Object[1] will be an Employee, and Object[0] will be the Dept that the Employee is in. This is a little over the top, because the employeeSet in each Dept will still fetch Employees, but it does make certain loops a little more readable.

Though I wrote my query the other way around, so for the way you wrote your query, the employee will be Object[0] and the dept will be Obect[1].


please tell me clearly, Could u send me how to get those two object in hibernate.

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 26, 2006 4:39 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
This is the code you wrote:
Code:
Iterator itrStud = students.iterator();
for( int index = 0 ; itrStud.hasNext() ; index++){
Object s1 = (Object) itrStud.next();
System.out.println("values :"+s1);
}
Change it to this:
Code:
Iterator itrStud = students.iterator();
for( int index = 0 ; itrStud.hasNext() ; index++){
Object[] a1 = (Object[]) itrStud.next();
System.out.println("values: " + a1[0] + " and " + a1[1]);
}


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