2

I am working on JPA project and I need your help.

I have two classes, “Person” and “Leader” which inherits from Person.

@Entity   
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)    
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)  
    private Long id;


    @Column(unique = true)    
    private String personId;

}

And

@Entity
public class Leader extends Person implements Serializable {

    private List < Person > listTeam;

    public void addPersonInTeam(Person e) {

        listTeam.add(e);
    }
}

My question Is, do I need to have JPA annotations @OneToMany or something else before private List listTeam in class Leader?

Thank you very much

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
TER
  • 57
  • 6
  • i suggest to read this http://stackoverflow.com/questions/2883033/is-it-possible-to-add-jpa-annotation-to-superclass-instance-variables , because i see much more issues than missing annotation on list. If it will not help, leave a note. – T.G Jul 13 '15 at 08:37
  • Do you mean that I need to put Person as abstract? or to add @MappedSuperClass? But in this case Person will not be persistant, although I need it to be persistant. Thank you very much for your help. – TER Jul 13 '15 at 09:04
  • 3
    Yes, of course you need a OneToMany or a ManyToMany annotation. Hibernate won't guess what the association is for you. – JB Nizet Jul 13 '15 at 09:07
  • 1
    Are you sure you want to extend? Maybe some simple boolean `isLeader` is enough in your case? – Aleksandr M Jul 13 '15 at 10:57
  • so you suggest that I have only the class Person which contains boolean isLeader and List which should be empty if the person is not leader? – TER Jul 13 '15 at 11:09
  • @AleksandrM Yes It's a better approach to avoid Inheritance. – cнŝdk Jul 13 '15 at 11:12
  • @TER: It depends. Maybe you need a `Team` entity too. – Aleksandr M Jul 13 '15 at 11:13

1 Answers1

2

You need to specify a mapping between the two classes because for Hibernate the association is not relevant here, you have to use annotations in both sides and I guess you will need a OneToMany mapping here :

Here's the mapping that you are seraching for:

In Person class:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String personId;

    @ManyToOne
    @JoinColumn(name="leader_id")
    private Leader leader;
    //getter and setter

}

In Leader class:

@Entity
public class Leader extends Person implements Serializable {

    @OneToMany(mappedBy = "leader")
    private List <Person> listTeam;
    //getter and setter

    public void addPersonInTeam(Person e) {
        listTeam.add(e);
    }
}

For further information you can see these links:

Note:

I don't see the use of the field personId in the Person class, there's no need to use two differents ids.

EDIT:

To answer your questions:

  1. The @JoinColumn(name="leader_id") is not mandatory, but it's used to specify the foreign key name.
  2. If the relation is ManyToMany the mappedBy property is used to specify the owner of the relationship, you can see this answer for more details.
Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Thank you very very much @chsdk, it's much more clear now, but I have additional questions: 1- @JoinColumn(name="leader_id") is mandatory is this case ? why ? 2- if the relation is ManyToMany (a person can be part of many leader's team), the mappedBy side is not that important? Whether it is in the Person or Leader side should work (but the performance will not be the same) right ? 3- the fact that Person is not abstract class is absolutely OK right? Thank you very very much – TER Jul 13 '15 at 11:19
  • Thank you very much chsdk, it's absolutely clear about the owner side of mappedBy, but you said "If the relation is ManyToMany the mappedBy property is optional," ? As I understand it's not very important which side is the owner in case of ManyToMany, but I need to choose a side right ? I mean if I leave only @ManyToMany in both sides without using mappedBy, it will not be a bidirectional link right? – TER Jul 13 '15 at 11:31
  • Sorry I don't think it's optional, my bad, you have to specify the owner of the relationship, I edited my answer. – cнŝdk Jul 13 '15 at 11:48