-->
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.  [ 5 posts ] 
Author Message
 Post subject: create and retireve a record
PostPosted: Sat May 19, 2007 12:16 pm 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2

Mapping documents:

Code between sessionFactory.openSession() and session.close():


Name and version of the database you are using: MySql

I am very new to hibernate!
How do I create a record in the DB and return it at the same time using the id. For example, if a I have Student with st_id I am trying to do this:
Code:
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
return student;


The stundent object that is returned doesn't containt the auto increment id field in the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 5:11 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

You can use session.get(Student.class,strID) and session.load(Student.class,strID). difference between these two is get() is always hit to database and load() is retrive objects from current session cache.

session.load(Student.class,strID);

Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 8:45 pm 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
OK, but will the object returned by load contain the auto incremented id that is generated by the DB ? Let's say I have a table of two fields; id and name only. When I save a new object I don't supply the id it's generated by the DB, How would I return this id? You said load retireves the object fro the cache, but the cached object doen't have the id ! May be I am wrong. Please confirm.
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 12:45 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

The id of the student object will fill after trans.commit() . So you can get id by student.getId()


Amila

(Don't forget to rate if it helps)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 7:03 am 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
Thank you.


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