-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate select query with 'where' clause
PostPosted: Tue Dec 23, 2008 1:48 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
I want to know how to write hibernate select query with where clause.


public String hibSelect(User tblObj, String sqlInsert) {


//sqlInsert = "SELECT * FROM mst_user WHERE user_id=?"

Session session = null;
String temp = "";
System.out.println(1);

AnnotationConfiguration ac = new AnnotationConfiguration();
session = ac.configure().buildSessionFactory().openSession();
if(session != null)
{
System.out.println("not null");
}
System.out.println(2);

SQLQuery query = session.createSQLQuery(sqlInsert);
System.out.println(3);

String name = tblObj.getUser_id();

query.setString(0, "ann");


System.out.println(4);

String q = query.toString();


Iterator iterator = query.iterate();

while(iterator.hasNext())
{
Object[] row = (Object[]) iterator.next();
temp = temp+":"+row[0];
}




return temp;
}

I wrote like this but this is not working.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 6:27 am 
Newbie

Joined: Tue Aug 19, 2008 9:30 am
Posts: 10
1) Is there any reason you use SQL and not HQL which is objectoriented and much easier?
2)I hope this just a testMethod and you don't create a new SessionFactory (and Configuration) every time you call your other methods.
3)you should close the session at the end.
4) you should probably use a transaction
Code:
Transaction transaction = session.beginTransaction();
...
transaction.commit();
session.close();

5)named queries ("...where user_id=:id") are much more readable than indexed queries ("...where user_id=?")
6)-If you try to get the one instance of User with this user_id, use
Code:
User user = (User)session.get( User.class,tblObj.getUser_id() );

-If user_id is not the primary id but denotes an association and you want all Users associated with the instance with that id, use this HQL-Querry:
Code:
List<User> users = (List<User>)session.createQuery( "from User user where user.user_id=:user_id" )
                                          .setString( "user_id",tblObj.getUser_id() )
                                          .list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 6:57 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
Hey Thanks Friend, It is really well explained.

Thank you again 'simon.void'.......

regards
Dilan Silva.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 7:24 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
I create a sessionFactory in every method which is for insert, update, delete and select.

As you say it is not suitable.

But If I created a sessionFactory in every method, I close them at the end of the method. so is there any wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 2:18 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
to 'simon.void'

one line solved every problems

User user = (User)session.get( User.class,tblObj.getUser_id() );

really appreciate your help....

regards
Dilan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 1:49 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
How do I apply this method to composite primary key ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 4:05 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
If you want to load an object with the composite id you just need to create new such object, define composite id properties and use it as an identifier.

For example if you have a class like
Code:
public class Person implements Serializable {

    private String name;
    private String surname;
    private long salary;
    // other properties

    // getters and setters
}


with the mapping like

Code:
<class name="Person">
        <composite-id>
            <key-property name="name"/>
            <key-property name="surname"/>
        </composite-id>
        <property name="salary"/>
        <!-- ... -->
    </class>


You can perform lookup by the following code:
Code:
Session session = ...;
Person id = new Person();
id.setName("Greg");
id.setSurname("House");
Person md = session.load(Person.class, id);


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