-->
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.  [ 4 posts ] 
Author Message
 Post subject: the query for selecting a specific item using session.find
PostPosted: Thu Mar 18, 2004 11:20 pm 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
I have this query to select a department name of a specific user.

The argument is name of the user, it would return the department object


User.JAVA
public class User
{
private Integer id;
private String userName;

//many other properties
private Department fkDept;

public Integer getId()
{
return id;
}

public void setId(Integer theId)
{
id = theId;
}

public String getUserName()
{
return userName;
}

public void setUserName(String theUserName)
{
userName = theUserName;
}

//many other properties's methods

public Department getFkDept() {
return fkDept;
}

public void setFkDept(Department theDepartment) {
fkDept = theDepartment;
}

}

Department.JAVA
public class Department
{
private Integer id;
private String departmentName;
private Set fkUser;


public String toString()
{
return "Department : " + departmentName;
}

public Integer getId() {
return id;
}

public String getDepartmentName() {
return departmentName;
}

public void setId(Integer theId)
{
id = theId;
}

public void setDepartmentName(String theDepartmentName)
{
departmentName = theDepartmentName;
}

public Set getFkUser() {
return fkUser;
}

public void setFkUser(Set theUsers) {
fkUser = theUsers;
}

}

Department.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native" />
</id>

<property name="departmentName" column="DEPARTMENT_NAME" not-null="true" />
</class>
</hibernate-mapping>

User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="User" table="USERS">
<id name="id" column="ID">
<generator class="native" />
</id>

<property name="userName" column="USERNAME" not-null="true" unique="true" />

<many-to-one name="fkDept" class="Department" column="DEPT_ID" not-null="true" />
</class>

</hibernate-mapping>


My query :

public String nameByUserName(String name) throws HibernateException
{
System.out.println("Retrieving the Department from a specific User");
sess = sessFact.openSession();
String deptName;
Transaction tx = null;
try
{
tx = sess.beginTransaction();
/*Start Query*/
String query =
"select dept from User as user inner join user.fkDept as dept "
+ "where lower(user.userName) = lower(:name) AND (user.fkDept.departmentName = dept.departmentName)";
deptList = sess.find(query, name,Hibernate.STRING);
/*End Query*/

if (deptList.size() == 0)
{
System.out.println("No department named " + name);
return null;
}
else
{
theDepartment = (Department)deptList.get(0);
deptName = theDepartment.getDepartmentName();
System.out.println("Department is successfully retrieved");
System.out.println(theDepartment);
}
tx.commit();

}
catch(HibernateException he)
{
if(tx!=null) tx.rollback();
throw he;
}
finally
{
closeSession();
}
return deptName;
}

I got a feeling what it actually does is comparing the DEPT_ID in USER of that USERNAME to itself!

"select dept from User as user inner join user.fkDept as dept "
+ "where lower(user.userName) = lower(:name) AND (user.fkDept.departmentName = dept.departmentName)";

user.fkDept is the same as dept?? Am I right? or since it's HIBERNATE we're talking about, should I just remove the part after AND coz it's useless??

coz in SQL it would be something like
select * from DEPARTMENT as dept, USER as user WHERE (user.USERNAME="John") AND (user.DEPT_ID=dept.ID);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 12:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
or since it's HIBERNATE we're talking about, should I just remove the part after AND coz it's useless??

yes.

All you need is:

select dept from User as user inner join user.fkDept as dept where lower(user.userName) = lower(:name)

Or, if your data is such that a user has to have a dept, simply do:

select user.fkDept from User as user where lower(user.userName) = lower(:name)

The second form is an implicit outer-join...


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Fri Mar 19, 2004 2:05 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
steve wrote:
All you need is:

select dept from User as user inner join user.fkDept as dept where lower(user.userName) = lower(:name)

Or, if your data is such that a user has to have a dept, simply do:

select user.fkDept from User as user where lower(user.userName) = lower(:name)

The second form is an implicit outer-join...


many thanks Steve.. you've cleared my doubt. :)

Which one is faster though? I bet the second one, eh?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 3:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
Which one is faster though? I bet the second one, eh?


try both and look at sql generated.... you'll have the answer


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