• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate does not save Object / Record

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pearlo Muthukumaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a

at the end, afer all transactions have been commited?
 
Pearlo Muthukumaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried:



See sections 14.3.1 and 14.3.2 in the on-line reference:
Hibernate Reference (chapter 14)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

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

Have you solved the problem? If yes, let me know how.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
Firefox Browser Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic