Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManager - persist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityManager - persist" Watch "EntityManager - persist" New topic

EntityManager - persist

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
I have a doubt,

if a Seller wants to sell an Item, it comes to a page & try to add Item

Case 1.
process finds that this is not registered seller, and provide seller a page to fill seller detail.
- Seller (object) Entity is created, but not attached
Seller fills detail of Item
- Item Enity (object) is created
Item has relationship with seller, so while creating Item one step is
- item.setSeller(seller);
final step
- em.persist(item);

Case 2.
process finds the seller
- Seller seller = em.find(Seller.class, sellerId);
- item.setSeller(seller);
- em.persist(item);

case 2 is from EJB 3 in Action page 315, case 1. is my asumption (if seller does not exist, and wants to add item).

EM will check if item PK does not exist, it will add Item to db in this case, but I am not sure how EntityManager will treat to Seller entity in Case 2. & Case 1.


Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
or may be case 1 is not good enough

it must have
Seller seller = new Seller();
- em.persist(seller); //if seller id is IDENTITY (auto generated) flush should needed to manage Seller
- em.flush();

- em.persist(item).
(now this Case 1 is also like Case 2 for EnetityManager).

Hope my assumptions are correct.

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
got it,

cascadetype.persist does exists...
It is sorta covered in the JavaRanch Style Guide.
subject: EntityManager - persist
Similar Threads
Query on Detached and Removed Entities
[JPA] Two providers, two different bahaviours - is switching really a piece of cake?
EJB 3 IN ACTION - EntityManager merge() and refresh() method conflict
Passed SCBCD 5.0 :)
Hibernate persistence not throwing EntityExistsException on persist