-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: OneToMany and abstract classes
PostPosted: Thu Dec 21, 2017 5:45 am 
Newbie

Joined: Wed Dec 20, 2017 11:18 am
Posts: 2
Hey,

I'm currently having a Problem (or maybe just a understanding mistake). However, I'm more or less a newby in relation to the JPA.
The Topic is a "@OneToMany"-Relation to a abstract class.

Here is a short example what I basically want to do:
Code:
@Entity
class MyMaster {
  @OneToMany
  List<MyAbstract> setOfAbstracts;
}

@MappedSuperclass
abstract MyAbstract  { ... }

@Entity
class MySlave1 extends MyAbstract { ... }

@Entity
class MySlave1 extends MyAbstract { ... }

Well, I (more or less) understand what is the problem with this solution. "MyAbstract" is no entity and therefore I could not genereate a "@OneToMany"-relation.
But what would be the correct way to do this (and how could I tell the code to do this)?

Here are some more detailed information about my project:
  • Basically, it is a object oriented programming project at a university. Therefore, the structure of the database is not as important as the structure of the objects. The current structure of the objects is the best one (from OOP-point-of-view). So I don't want to change anything in the object-structure, even if the database is getting quite big and and unhandy.
  • One "MySlave"-Object can only refer to one "MyMaster" (I don't have to block other relations, but maybe it's an helpfull information)
  • Obviously, One "MyMaster" relates to many "MyAbstracts"
  • In my project there are more than two "MySlaves"

I thought about a few solution, but don't know how the realize theme or they are not suitable for my application.
Solution 1: Two list in "MyMaster" to seperate the "MySlaves":
This is no solution, because I can't change the structure of the list in "MyMaster"

Solution 2 (from database point of view): A FK in the "MySlaves" to "MyMaster"
Basically, this would be a @ManyToOne from "MyAbstract" to "MyMaster". But how can I than (automatically) create the list of "MyAbstract" in "MyMaster"?

Solution 3 (from database point of view): A sperate entity "MyAbstract":
In the database this would be a seperate table for the fields of "MyAbstract" which points to "MySlave1" or "MySlave2" with the additional fields of them. Then I could relay have a @OneToMany in "MyMaster". However, I have no idea how I could tell the code to do this.

Can anyone please help me with a hint or a solution?
Thanks!

fhv_fb


Top
 Profile  
 
 Post subject: Re: OneToMany and abstract classes
PostPosted: Thu Dec 21, 2017 9:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Because MyAbstract is a MappedSuperclass, it can't be used as a relation.

Solution 3 looks ok. Read this article too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.