• 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

EntityManager - persist

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks....

 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or may be case 1 is not good enough

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

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

Hope my assumptions are correct.

 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it,

cascadetype.persist does exists...
reply
    Bookmark Topic Watch Topic
  • New Topic