Forums Register Login
Hibernate not saving
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()):


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" />

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.

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)


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
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..

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
Yup, that was it!!!

Thanks very much to all who replied!!!


You are more than welcome.

-Cameron McKenzie
[ April 24, 2008: Message edited by: Cameron Wallace McKenzie ]
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

This thread has been viewed 2440 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 17, 2019 12:35:13.