*
The moose likes Object Relational Mapping and the fly likes Hibernate does not save Object / Record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate does not save Object / Record" Watch "Hibernate does not save Object / Record" New topic
Author

Hibernate does not save Object / Record

Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Hi all,
I have written a simple example of storing "Cat" object in the Hibernate Example. I have written the client code as foloows in the servlet:
...........
...........
Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);

System.out.println("Hello World 1");
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
System.out.println("Hello World 2");
Session sess = sessionFactory.openSession();

Transaction tx= sess.beginTransaction();
System.out.println("Creating Cat");


sess.save(princess);
tx.commit();
System.out.println("Committing Cat...");
sess.close();
............
............

All the messages are displayed in the server console properly and no exception messages are observed. But the database I cannot find the record in the database.

I have hibernate.cfg.xml and Cat.hbm.xml in the WEB-INF/classes directory and I have also placed the servlet in the same directory. I have placed hibernate2.jar and other jars appearing in lib directory of hibernate installation in WEB-INF/lib directory.

What could be the problem? Am I committing a blunder?

Regards
Muthu
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Enable hibernate debugging (read the documentation to tell you how to do this) and watch for the insert statement. You might spot some errors there. Also watch the log to make sure your Cat objct has mapped properly, you will get very obvious errors if it hasn't. One last, what PK strategy are you using: generated? assigned? YOu might want to post your mapping file so we can see.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Thank you paul,

Here is my

hibernate.cfg.xml
=================
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.datasource">java:comp/env/jdbc/iDENOraDS</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</property>

<!-- Mapping files -->
<mapping resource="Cat.hbm.xml"/>

</session-factory>

</hibernate-configuration>


and my

Cat.hbm.xml
===========
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="Cat" table="CAT">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>

<property name="sex"/>

<property name="weight"/>

</class>

</hibernate-mapping>

If you observe hibernate.cfg.xml, I have already set show_sql=true and SQL statement I got through Tomcat console is as following:

Hibernate: insert into CAT (NAME, sex, weight, CAT_ID) values (?, ?, ?, ?)

Which is perfect.

Even I had the same doubt as that of yours as to whether Oracle supports a "uuid.hex" kind of support for id generation, so I tried changing it to Oracle Sequence (regenerated my Schema using SchemaExport tool) and changing id strategy to "sequence", still the solution did not work. It silently executes all the debug statements I had included in servlet and does not throw any exceptions and ALSO NOT STORING THE "CAT"

Any clues?

Regards
Muthu
Andrei Stroe
Greenhorn

Joined: Jun 01, 2004
Posts: 1
Do you have a

at the end, afer all transactions have been commited?
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Hi,

I tried

session.saveOrUpdate(<object> ;
session.save(<object> ;

session.flush();

but the behaviour has not changed. No exceptions, no additional messages and no saving of "CAT"

Regards
Muthu
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Can you set a profiler/trace tool running on your DB and see what activity Hibernate is generating? Or checked the sequence to see if the value is advancing (i.e. something has actually secected something from it)? Its very odd you see no exception.
Mridul Paliwal
Greenhorn

Joined: Jun 08, 2004
Posts: 2
hi,

I am seeing the same behavior.
Did u figure out what was wrong?

like mentioned in the post, the insert statements do show in the debug.
I am guessing
- Transaction Manager (using default)
- Caching (using default)
- hashcode and equals implementations


I also do get the object back, but it's just not persisted to the database.
basically this test succeeds

public void testInsert() throws DataAccessException{
String strName = "From Test ";
Account a = new Account();
a.setAccountName(strName);
Account ret = (Account) new AccountDAO().insert(a);
assertNotNull(ret.getObjectId());

Account ar = new AccountDAO().getByName(strName);
assertEquals(strName,ar.getAccountName());
}
Mridul Paliwal
Greenhorn

Joined: Jun 08, 2004
Posts: 2
Wrapping it in a transaction did it for me.
But it was working without it earlier.
I guess i'll have to dig deeper into Hibernate.

Also, Although i invoke flush here, it worked even without it.

Account a = new Account();
a.setAccountName("Direct Create");
a.setAccountType("standard");
try{
Session s = HibernateSession.currentSession(null);
Transaction t = s.beginTransaction();
HibernateSession.currentSession(null).save(a);
HibernateSession.currentSession(null).flush();
t.commit();
}catch(Exception e){
e.printStackTrace();
}
roger wallace
Greenhorn

Joined: Jun 08, 2005
Posts: 1
Have you tried:



See sections 14.3.1 and 14.3.2 in the on-line reference:
Hibernate Reference (chapter 14)
Jose Naves
Greenhorn

Joined: May 16, 2004
Posts: 2
Hello!

I'm facing a similar problem as you in http://www.coderanch.com/t/214825/ORM/java/Hibernate-does-not-save-Object

Have you solved the problem? If yes, let me know how.


Jos´┐Ż Naves de Moura Neto
Chris Romann
Greenhorn

Joined: Mar 29, 2006
Posts: 1
Hi guys,

After we migratged from Hibernate 2 to Hibernate 3 we were facing the same problem. What helped us was stated in the last statement: use session.flush() before commit.
Therefore our (now running) code looks like this:

Tournament t = new Tournament();
t.setTournamentName("New Tournament");
t.setTournamentType("Hi / Low");
try{
Session s = HibernateSession.currentSession(null);
Transaction t = s.beginTransaction();
s.save(t);
s.flush();
t.commit();
s.close();
}catch(Exception e){
e.printStackTrace();
}

Hope you guys get your stuff up and running as well now.

Regards

Chris
Prabu Pidaran
Greenhorn

Joined: Feb 22, 2010
Posts: 19
Hi all..

I am having the same problem after i did session.flush() and t.commit(). the code throws no exception and am able to get back the new object after inserting the value. But the vaules are not reflected in the DB. We are using hibernate 3. any help?
Juan Duran
Greenhorn

Joined: Apr 19, 2010
Posts: 1
I had the same problem and as stated above the problem was not calling commit. Turning on the mysql query log helped a lot as I could actually see the 'rollback' thrown by hibernate.
Prakash Lightning
Greenhorn

Joined: Sep 28, 2012
Posts: 1

I had a similar problem , Where I did not begin the transaction using Transaction t=session.beginTransaction();
then added line consisting tx.commit(); after the obj.save(); I am able to see the changes committed to the database.


Thanks,
Prakash
Falling down is not defeat, defeat is when you refuse to get up.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate does not save Object / Record
 
Similar Threads
Hibernate.cfg.xml and hibernate.properties
Problem in building hibernate SessionFactory
NoClassDefFoundError:
Problem with running my first Hibernate App
Mapping exception - cannot find Message.hbm.xml