Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Choosing the right strategy for JPA Inheritance Mapping
PostPosted: Tue Apr 04, 2017 8:10 pm 
Newbie

Joined: Tue Apr 04, 2017 8:04 pm
Posts: 2
Hi all, i've read some tutorials for Inheritance Mapping, which couldnt quitely solve my questions.

Say i have an abstract class : User
And 3 more other subclasses : UserA, UserB, UserC, those all extend User.

Every Subclass has its own table, Superclass User, meanwhile, doesn't.

In my other class Website i have a ArrayList, or should i say Collections, of Users.

The list should fetch all users of the Website.

Which strategy should i use ? I thought of MappedSuperclass, but since in my Website class the List is of User type, so am not sure what to do here.

Thanks for any help!


Top
 Profile  
 
 Post subject: Re: Choosing the right strategies for inheritance mapping.
PostPosted: Wed Apr 05, 2017 1:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1092
If you use @MappedSuperclass, then the inheritance model is materialized only in Java, but not the DB. Hence, you cannot run polymorphic queries or map polymorphic associations.

The only reasonable way to make it work is to use JOINED inheritance here and accept the fact that the common columns will be mapped to the base class associated table.

As I explained in my book, you should never use TABLE_PER_CLASS because it's not very efficient in terms of SQL statements. While JOINED is probably the first choice for your mapping requirements, if the subclasses don't differ too much, then you should consider using SINGLE_TABLE inheritance which is the most efficient inheritance strategy.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Choosing the right strategy for JPA Inheritance Mapping
PostPosted: Wed Apr 05, 2017 5:31 am 
Newbie

Joined: Tue Apr 04, 2017 8:04 pm
Posts: 2
Hi, i have read some examples about Joined Inheritance, sadly it requires that my SuperClass must have its own table, which i dont have. Am i forced to use @MappedSupperclass in this case?


Top
 Profile  
 
 Post subject: Re: Choosing the right strategy for JPA Inheritance Mapping
PostPosted: Wed Apr 05, 2017 5:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1092
If you cannot alter the database, then you will need to use @MappedSuperclass and you will not be able to do polymorphic queries and associations.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.