Reputation: 21608
I want to split an existing JPA-Entity into a POJO superclass and an Entity subclass. I want to put the POJO superclass into a library project, that can be referenced by other projects, that do not use JPA.
My old/existing code successfully declared a OneToMany-relationship like this:
@Entity
public class Person {
@OneToMany(mappedBy="petOwner")
public List<Pet> pets = new ArrayList<>();
}
I want to split it into this superclass:
public class CommonPerson {
public List<Pet> pets = new ArrayList<>();
}
The question: How could a appropriate jpa subclass look like? Can I set mappedBy
in a subclass?
What I tried:
@Entity
// not possible: @AttributeOverride (has no "mappedBy" or "OneToMany")
// not possible: @AssociationOverride (has no "mappedBy" or "OneToMany")
public class JpaPerson extends CommonPerson {
}
I'm using Hibernate JPA api 2.1.
Upvotes: 2
Views: 732
Reputation: 558
If you use the @Transient annotation in the MappedSuperclass method, you can use any other JPA annotation in the same method's subclass. So you can 'extend' the JPA annotations in the subclass.
Upvotes: 0
Reputation: 21608
Thanks @petros-splinakis !
I'm now using something like this:
@Entity
@Access(AccessType.PROPERTY)
public class JpaPerson extends CommonPerson {
@OneToMany(mappedBy="petOwner")
public List<Pet> getPets() {
return pets;
}
public void setPets(List<Pet> pets) {
this.pets = pets;
}
}
And it works like a charm!
Upvotes: 1