[Logo]
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()):

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

Thanks very much to all who replied!!!

MUCH APPRECIATED.

You are more than welcome.

-Cameron McKenzie
[ April 24, 2008: Message edited by: Cameron Wallace McKenzie ]
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 1814 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 21, 2018 18:09:01.