This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Persisting Polymorphic Entities  RSS feed

 
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: 4105
34
  • 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
Boost this thread!