This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Hibernate not saving  RSS feed

 
Ranch Hand
Posts: 1611
13
  • 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
 
ranger
Posts: 17344
11
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: 1611
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
author and cow tipper
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
 
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
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: 1611
13
  • 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
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 ]
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!