• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Query

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Repeating the exact same text doesn't clarify anything, really.
 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. You could have just checked the "disable HTML" checkbox too.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try using

Sarma
 
deepa karra
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sarma,
Thankyou. I tried using sQLQuery. It just displays a blank record
 
Raza Mohd
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic