Mapping bidirectional one-to-one relationship fails with an AnnotationException: Unknown mappedBy in: / referenced property unknown

Description

Mapping a simple bidirectional one-to-one relationship fails with an AnnotationException for no obvious reason.

DB design:

CREATE TABLE Persons
(
id INTEGER NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE Coaches
(
id INTEGER NOT NULL,
license_nbr VARCHAR(10) DEFAULT NULL NULL,
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES Persons (id)
);

CREATE TABLE Players
(
id INTEGER NOT NULL,
registration_nbr VARCHAR(20) DEFAULT NULL NULL,
PRIMARY KEY (id),
FOREIGN KEY (id) REFERENCES Persons (id)
);

Mappings (relevant shown only):

@Entity
@Table(name = "Persons")
public class Person implements Serializable
{
@Id
@Column(name = "id")
private Integer id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@OneToOne(mappedBy = "person")
private Coach coach = null;

@OneToOne(mappedBy = "person")
private Player player = null;

...
}

@Entity
@Table(name = "Players")
public class Player implements Serializable
{
@Column(name = "registration_nbr")
private String registrationNbr = null;

@Id
@OneToOne
@JoinColumn(name = "id")
private Person person = null;

...
}

@Entity
@Table(name = "Coaches")
public class Coach implements Serializable
{
@Column(name = "license_nbr")
private String licenseNbr = null;

@Id
@OneToOne
@JoinColumn(name = "id")
private Person person = null;

...
}

The exception thrown in a test app:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: persons] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at tld.persons.Main.main(Main.java:34)
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: tld.persons.model.Person.coach, referenced property unknown: tld.persons.model.Coach.person
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:159)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1686)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)
... 4 more

This exception is really surprising given the simplicity of this design. I've also tested this with EclipseLink which has no problems with the mappings.

I'll attach an test case including the full code.

Activity

Brett MeyerMarch 7, 2014 at 5:31 PM

Bulk closing rejected tickets in "resolved" state.

Ka WuMay 2, 2011 at 1:56 PM

Sorry bad research on my part!: this is likely a duplicate of http://opensource.atlassian.com/projects/hibernate/browse/HHH-5695
Won't attach a test case then.

Duplicate

Details

Assignee

Reporter

Components

Affects versions

Priority

Created May 2, 2011 at 1:43 PM
Updated March 7, 2014 at 5:31 PM
Resolved August 17, 2011 at 1:51 AM