-->
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.  [ 4 posts ] 
Author Message
 Post subject: How this declaration returns an integer?
PostPosted: Fri Apr 12, 2013 6:23 am 
Newbie

Joined: Fri Apr 12, 2013 1:53 am
Posts: 18
The following is a segment of code in an Application class of Hibernate. How come, this ME.addEmployee returns an integer?
Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
Integer empID3 = ME.addEmployee("John", "Paul", 10000);

The following is the code for addEmployee;

public Integer addEmployee(String fname, String lname, int salary)
{
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try
{
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
tx.commit();
}catch (HibernateException e)
{
if (tx!=null) tx.rollback();
e.printStackTrace();
}
finally
{
session.close();
}
return employeeID;
}


Top
 Profile  
 
 Post subject: Re: How this declaration returns an integer?
PostPosted: Fri Apr 12, 2013 6:28 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
Your question is somewhat unclear to me.
Looking at the code u are using session.save which returns the the id of entity persisted in the database.

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: How this declaration returns an integer?
PostPosted: Fri Apr 12, 2013 7:00 am 
Newbie

Joined: Fri Apr 12, 2013 1:53 am
Posts: 18
Akash Mittal wrote:
Your question is somewhat unclear to me.
Looking at the code u are using session.save which returns the the id of entity persisted in the database.


Initially the values in databases are null then how session.save can return the id?
I think session.save saves the details of the employee(fname, lname, salary) into the database.

Its all confusing. can anyone tell me what session.save() actually does?


Top
 Profile  
 
 Post subject: Re: How this declaration returns an integer?
PostPosted: Fri Apr 12, 2013 7:16 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
Code:
save
Serializable save(Object object)
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update"
Parameters:
object - a transient instance of a persistent class
Returns:
the generated identifier


In layman if your generator class is native/sequence/etc then its hibernate job to save the entity in database with a valid key.
Once it save in database it returns the primary key /id that it used earlier.
In case generator class is assigned it will return the same key /id provided you..

Hope its clear now

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


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