This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to persist an Object" Watch "How to persist an Object" New topic
Author

How to persist an Object

Pramod Kumar
Ranch Hand

Joined: Oct 05, 2007
Posts: 40
Hi,

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.

Thanks,
Paul Sturrock
Bartender

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
    
    1

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Ouch.

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.

Mark


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
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to persist an Object
 
Similar Threads
Persistence of updated objects
How do you update a collection if some exist and some are new?
save entity with SYSDATE
serializable object
alternative to session.flush() in hibernate?