Reputation: 54796
I'm running into some frustrating behavior with Hibernate when trying to set up my schema using annotations. Let's say that I have an entity that will have one-to-many relationships to several other types. For example:
@Entity
@Table(name = "clubs")
public class Club {
private long id;
private String name;
//...
private Collection<ClubLocation> locations;
public Club() {
this.name = null;
//...
this.locations = Collections.emptyList();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy = "club")
public Collection<ClubLocation> getLocations() {
return locations;
}
public void setLocations(Collection<ClubLocation> locations) {
this.locations = locations;
}
}
Now as I mentioned I want to associate this entity with a number of other entities, so it seemed like a good idea to define a superclass entity that manages the relationship back to Club
so that I don't need to set the relationship up in each associated class. I do this using:
@Entity
@Table(name = "clubItems")
@Inheritance(strategy = InheritanceType.JOINED)
public class ClubItem {
protected Club club;
public ClubItem() {
this.club = null;
}
@ManyToOne(optional = false)
public Club getClub() {
return club;
}
public void setClub(Club club) {
this.club = club;
}
}
And then I extend that class like:
@Entity
@Table(name = "clubLocations")
public class ClubLocation extends ClubItem {
private String title;
//...
public ClubLocation() {
this.title = null;
//...
}
@Column(nullable = false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
All seems well, except when I try to run and then Hibernate explodes with:
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: ClubLocation.club in Club.locations
[WARNING] [talledLocalContainer] at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:543)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:508)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1127)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1112)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:183)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:240)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:120)
[WARNING] [talledLocalContainer] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
[WARNING] [talledLocalContainer] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
Frustratingly, if I duplicate the getter and setter declarations for the club
property inside of the ClubLocation
class, all is well. Specifically, it works find if I add:
@Override
@ManyToOne(optional = false)
public Club getClub() {
return super.getClub();
}
public void setClub(Club club) {
super.setClub(club);
}
...into the ClubLocation
class. But that's pointless because all it does is delegate to the superclass.
Is Hibernate just not sophisticated enough to handle the inherited mapping field without the getter/setter being redeclared in the subclass? If Hibernate can handle this case more gracefully, how does it need to be set up/what am I doing wrong?
Upvotes: 0
Views: 336
Reputation: 42074
What it comes to JPA, your mappings are perfectly valid. Also for example in EclipseLink it works as it is. This is problem in Hibernate's implementation.
This blog post provides kind of workarounds. Unfortunately all of those are imperfect: changes in table structure, loosing type to query with mapped superclass, etc.
Upvotes: 1
Reputation: 4022
I've got a similar setup in a project. I think an easy and more-or-less nice way to solve this is to change
private Collection<ClubLocation> locations;
into
private Collection<ClubItem> items;
(and the associated getter, setter, too). This way it surely works.
If you need a getLocations method in Club for convenience reasons, your are free to create that. If you are sure, that items will contain only locations, you can return the collection as it is, otherwise you'd need to filter it in Java code.
A disadvantage is that if items contain also non-location elements, filtering in Java takes memory and CPU, so it is potentially slow.
Upvotes: 1