-->
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.  [ 3 posts ] 
Author Message
 Post subject: Why Do I need to call save to make persistent in this case?
PostPosted: Sat Jun 11, 2005 2:10 am 
Newbie

Joined: Thu May 19, 2005 11:50 pm
Posts: 10
I have the book "Hibernate in Action". I finally got hibernate configured to work but now my understanding from the book differs from what I see happening when I run the application. I thought that when I created a new User object that it would be persisted without me having to call session.save(user) because these columns are specified in file User.hbm.xml. I noticed that when I call session.save(user) it saves a row in the Users table. If I don't call it then then that table has 0 rows. I should note that I have the following in my configuration...

Code:
cfg.setProperty("hibernate.hbm2ddl.auto", "create");




Hibernate version:
3.0

Mapping documents:
User.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.business.service.hib3">
   <class name="User" table="USERS">
      <id name="id" column="USER_ID">
         <generator class="native"/>
      </id>
      <property name="username" column="USERNAME"/>
      <property name="password" column="PASSWORD"/>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user.setUsername(username);
user.setPassword(password);
//session.save(user); // why do I need this???
tx.commit();
session.close();

Full stack trace of any exception that occurs:
None

Name and version of the database you are using:
My SQL 4.1.9

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Code:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 11, 2005 2:18 am 
Newbie

Joined: Thu Jun 09, 2005 2:49 pm
Posts: 6
The object cannot even be handled by Hibernate until you give it a reference to it. There is not some magic instrumentation that hibernate puts in your classes to make them be "persistent". That is one of the attractions of hibernate for me, in particular. It means that the beans I use to persist to the database can also be used outside of the context of a session and therefore passed to layers of my application that know nothing about hibernate or that there is a database anywhere around.

Read your code again, at what point does a hibernate API get a hold of a reference to your newly created object? Not until you call session.save(). Up to that point, your object is just a POJO.

Hope this helps clear things up.


Top
 Profile  
 
 Post subject: Makes Sense
PostPosted: Sat Jun 11, 2005 2:35 am 
Newbie

Joined: Thu May 19, 2005 11:50 pm
Posts: 10
That makes a lot of sense actually. Thank you for helping my Hibernate newbie self out. :)


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