Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persisting Polymorphic Entities

 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to persist polymorphic entities?

For example consider three entities A, B and C:
Class A
Class B extends A
Class C extends A

A has 3 properties while B & C add 2 more different properties.

In my session bean, I want to do only a single em.persist(A) to persist all the 3 kinds of entities coming from the client i.e A, B or C. Is it possible? Or should I write 3 different persisting functions namely:

em.persist(A)
em.persist(B)
em.persist(C)

This, in my opinion disregards polymorphism.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin,
When you persist, you persist an object. So when you call em.persist(x), x could be an object of any class ( A, B, or C) and all fields of that class will be persisted in respective table(s) as per the design. In that sense, you don't have to worry about the type of the variable or the class of the actual object. You just call em.persist(x).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic