aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate not saving 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 not saving" Watch "Hibernate not saving" New topic
Author

Hibernate not saving

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I have a simple situation: a single table in MySQL with a primary key set to autogenerate.

However, when I issue code like this (after doing all the setters to set the newUser fields after Users newUser = new Users()):

dao.save(newUser);

A new record does *not* get added to the database.

My users.hbm.xml file has the following for the user PK:

<id name="usrPk" type="java.lang.Integer">
<column name="usr_pk" />
<generator class="identity" />
</id>

My understanding is that the generator class="identity" means that MySQL (5.0x) will create the required PK.

However since a record is not being added, I'm not sure what could be wrong.

Reading from the database using the same dao works fine.

I also have

<property name="hibernate.show.sql">true</property>

set in the hibernate config file, but it doesn't show any sql at all in the output window in Eclipse.

Any advise or suggestions would be appreciated.

Thanks...Mike
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

What does your code look like? Are you opening a session and starting a transaction, then after you create your object are you calling saveOrUpdate() in the Session object or if using JPA calling persist(), then after that committing the transaction?

Please post your code. (Use the code button below to keep the formatting and indentation of code)

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

You might also want to make sure you're catching any exceptions and gathering information from them. You should be getting some feedback if your code isn't working. Do you know that the code is even being encountered?

-Cameron McKenzie
Pedro Erencia
Ranch Hand

Joined: Apr 03, 2008
Posts: 70
Though i'm lacking BaseHibernateDAO code, it seems that the code lacks the transaction demarcation.. It should be something like this..



Maybe you can consider HibernateTemplate from Spring..
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Indeed.

Even though the Data Access Object will hide much of the Hibernate plumbing from the developer, transaction demarcation is still the responsibility of the integrator who is using the DAO.

You've got to start the transaction before using the DAO, and commit the transaction when you are done.

-Cameron McKenzie
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Yup, that was it!!!

Thanks very much to all who replied!!!

MUCH APPRECIATED.

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

You are more than welcome.

-Cameron McKenzie
[ April 24, 2008: Message edited by: Cameron Wallace McKenzie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate not saving