wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Query" Watch "Hibernate Query" New topic
Author

Hibernate Query

deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
Hi,
I am retreiving values from a database based on a parameter using hibernate and displaying the data using struts.
The query i am using is

return session.createQuery("select a from Users as a where a.USERNAME like ?").setString(0,userName).list();

The code of the display page is

<s:iterator value="userList" status="userStatus"> </s:iterator>
<s:property value="USERNAME"/> <s:property value="ID" />



The above code works fine. When i try to display only the username, using select a.USERNAME from Users as a where a.USERNAME like ? in the query, i don't see any records.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Can you see you post wasn't formatted the way you intended?

You'll have to be more precise--I don't understand your question.
deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
Hi,
I am retreiving values from a database based on a parameter using hibernate and displaying the data using struts.
The query i am using is

return session.createQuery("select a from Users as a where a.USERNAME like ?").setString(0,userName).list();





The above code works fine. When i try to display only the username, using select a.USERNAME from Users as a where a.USERNAME like ? in the query, i don't see any records.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Repeating the exact same text doesn't clarify anything, really.
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

hi deepa,

can you tell me which one works fine?
and what problem are you having?


Good luck!!
A small leak can sink a Gigantic ship.>
deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
hi Raza Mohd ,

I am using the following query to retreive values from the database and it displays all the columns from the table User


session.createQuery(select a from User a where a.name like 'c%').list();



I am unable to retreive a single column from the database using the following query

session.createQuery(select a.name from User a where a.name like 'c%').list();



deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
David Newton,
Thankyou. I posted the question again, as the formatting was not correct the first time.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh. You could have just checked the "disable HTML" checkbox too.
sarmaP lolla
Greenhorn

Joined: Feb 01, 2009
Posts: 18
try using

Sarma
deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
sarma,
Thankyou. I tried using sQLQuery. It just displays a blank record
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

hi deepa,

Try to execute query (that hibernate fires ) from console to Query browser of Database.
It will help you to experiment what is actually happening.Otherwise the syntax is pretty much okay.

regards
Raza!
sarmaP lolla
Greenhorn

Joined: Feb 01, 2009
Posts: 18
When you are selecting individual properties you need to either transform it to domain object using transformation or you need to access the value by typecasting it Object[]. Try the following code.


- Sarma
deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
Sarma,
I changed the query and now I have a different error, java.lang.ClassCastException: java.lang.String cannot be cast to User1

Here is my code



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class UserDAOImpl implements UserDAO {

@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;

@SuppressWarnings("unchecked")
@Override
public List<User1> listUser() {
List<User1> courses = new ArrayList();


List<User1> q = (List<User1>)session.createQuery("select u.name from User1 u").list();

for (Iterator iter = q.iterator(); iter.hasNext();) {
courses.add((User1) iter.next());





}


return courses ;
}

@Override
public void saveUser(User1 user) {
try {
session.save(user);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}


}

sarmaP lolla
Greenhorn

Joined: Feb 01, 2009
Posts: 18

You can not use this statement.


The reason is u.name is a String. You are no longer getting a User1 object from the query. If you still want to work with User1 objects then you need to use transformation as shown in the example below.



- Hope this helps.
Sarma
deepa karra
Ranch Hand

Joined: Apr 04, 2008
Posts: 46
Hi Sarma,
Thankyou very much. The program is working.
It works fine if querying from a single table.
How can I write the query to retreive from more than one table(may be 2 or 3) and with parameters? For Example if my query is

session.createQuery("select a.name , b.dept from User as a,department as b where a.DEPT_ID = b.DEPT_ID and a.NAME like ?").setString(0,userName).list();

Thanks,
Deepa
 
wood burning stoves
 
subject: Hibernate Query
 
Similar Threads
iterate an object in struts-2
struts2 custom components checkbox JSP
Using Struts to iterate over queries with aggregates
Struts 2 problem -- s:hidden
how to display mysql table datas in jsp using struts2