Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Query

 
deepa karra
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repeating the exact same text doesn't clarify anything, really.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi deepa,

can you tell me which one works fine?
and what problem are you having?
 
deepa karra
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton,
Thankyou. I posted the question again, as the formatting was not correct the first time.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. You could have just checked the "disable HTML" checkbox too.
 
sarmaP lolla
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using

Sarma
 
deepa karra
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sarma,
Thankyou. I tried using sQLQuery. It just displays a blank record
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic