我正在使用Hibernate进行练习,但在实现一对一关系方面已经失败了好几个小时了。我在stackoverflow上阅读了很多答案,并完成了2个教程(mkyong和其他一些人),但我不知道哪里出错了。
我尝试了很多方法,但最终都会遇到同样的异常(org.hibernate.AnnotationException: Unknown mappedBy in: ch.myapp.model.Employee.office, referenced property unknown: ch.myapp.model.Office.EMPLOYEES
)
如果有人能给我一个提示问题所在,我会非常高兴。
我正在使用这个数据库模式,并尝试在Office和Employee之间实现1:1的关系(我知道这没有太多意义)。
Employee.class
@Entity
@Table(name = "EMPLOYEES")
public class Employee {
@Id
@GeneratedValue
@Column(name = "EMPLOYEENUMBER")
private Integer employeeNumber;
@Column(name = "FIRSTNAME")
private String firstName;
@Column(name = "LASTNAME")
private String lastName;
@Column(name = "EXTENSION")
private String extension;
@Column(name = "EMAIL")
private String email;
@Column(name = "JOBTITLE")
private String jobTitle;
@Column(name = "OFFICECODE")
private String officeCode;
// ACHTUNG, Test "EmployeeDataAccesTest -> loadEmployee()" greift auf einen
// Null-Wert zurück. Null kann keinem primitiven Wert zugeordnet werden.
@Column(name = "REPORTSTO")
private Integer reportsto;
@OneToOne(mappedBy = "EMPLOYEES", fetch = FetchType.EAGER)
private Office office;
//Getters and setters without annotations...
}
Office.class
@Entity
@Table(name = "OFFICES")
public class Office {
@Id
@Column(name = "OFFICECODE")
private String officeCode;
@Column(name = "CITY")
private String city;
@Column(name = "PHONE")
private String phone;
@Column(name = "ADDRESSLINE1")
private String addressLine1;
@Column(name = "ADDRESSLINE2")
private String addressLine2;
@Column(name = "STATE")
private String state;
@Column(name = "COUNTRY")
private String country;
@Column(name = "POSTALCODE")
private String postalCode;
@Column(name = "TERRITORY")
private String territory;
@OneToOne
@JoinColumn(name = "OFFICECODE")
private Employee employee;
//Getters and setters without annotations...
}
Hibernate.cfg.xml:
l version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="hibernate.connection.url">jdbc:derby:/home/dev/dev/git/TestBusiness/myDB</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="ch.myapp.model.Office"/>
<mapping class="ch.myapp.model.Employee"/>
</session-factory>
</hibernate-configuration>
This is the Stacktrace I get (first couple of lines):
org.hibernate.AnnotationException: Unknown mappedBy in: ch.myapp.model.Employee.office, referenced property unknown: ch.myapp.model.Office.EMPLOYEES
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:154)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1659)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1634)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at ch.myapp.HibernateManager.getSessionFactory(HibernateManager.java:44)
at ch.myapp.HibernateManager.openCurrentSessionwithTransaction(HibernateManager.java:24)
at ch.myapp.dataaccess.EmployeeDataAccess.load(EmployeeDataAccess.java:18)
at ch.myapp.dataaccess.OfficeDataAccessTest.shouldLoadOffice(OfficeDataAccessTest.java:24)