• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate not saving

 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that was it!!!

Thanks very much to all who replied!!!

MUCH APPRECIATED.

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are more than welcome.

-Cameron McKenzie
[ April 24, 2008: Message edited by: Cameron Wallace McKenzie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic