This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes How to persist an Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to persist an Object" Watch "How to persist an Object" New topic

How to persist an Object

Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40

I need to modify a record in db, I have class with getters and setters and I am persisting this Object to db, I am able to insert data thru em.persist(user);

user is the object of that class, now I need to update the data in db I am using em.merge(user); now throwing IllegalArgumentException and think that object need to be persisted before doing update. How to persist this object.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Is this Hibernate we are talking about? Or another ORM?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40
This is in Hibernate EntityManager and JPA
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Given this simple JPA annotated POJO:

It can be persisted to the database, updated, retrieved and deleted with the following code which focusses on the basic CRUD operations:

Is that what you were looking for?
[ October 25, 2007: Message edited by: Cameron McKenzie ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249


In Cameron's example he is creating a SessionFactory in each method. In a production application you will want to only create it once and have the entire application share that one and only one instance. As SessionFactory objects are heavy-weight objects. Now Session objects are light-weight.

And in defense of Cameron's example, there really is only one mapped class, so the SessionFactory isn't as heavy as it would be in a real application.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Good point, Mark. There's actually a chapter in my Hibernate book that explores just that point, along with other optimizations around the SessionFactory, Session, and the Hibernate Core in general.

The example is purely for andragogical purposes. Each method is contains all of the information needed to perform the database interaction, without calling any helper classes or singleton methods or anything like that.

When you're learning, I think it's good to see as much of the plumbing as possible. In production, it's another story.

-Cameron McKenzie
jQuery in Action, 2nd edition
subject: How to persist an Object
Similar Threads
serializable object
alternative to session.flush() in hibernate?
save entity with SYSDATE
How do you update a collection if some exist and some are new?
Persistence of updated objects