• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to persist an Object

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this Hibernate we are talking about? Or another ORM?
 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is in Hibernate EntityManager and JPA
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic