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.
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.
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop