我在休眠期间锻炼,而且我在实施OneToOne关系时已经失败了几个小时。我在stackoverflow上阅读了很多答案并经历了2个教程(mkyong和其他人),但我不知道我做错了什么。
我尝试了很多东西,但我总是遇到同样的异常(org.hibernate.AnnotationException: Unknown mappedBy in: ch.myapp.model.Employee.office, referenced property unknown: ch.myapp.model.Office.EMPLOYEES
)
如果有人能给我一个问题所在的暗示,我会很高兴。
我正在使用this database scheme,我尝试在Office和员工之间实现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>
这是我得到的Stacktrace(前几行):
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)
答案 0 :(得分:4)
您在Employee.class中有错误的mappedBy值,它应该是以下内容:
fopen
答案 1 :(得分:4)
1.确保属性如下:
@OneToOne(mappedBy = "employee", fetch = FetchType.EAGER)
private Office office;
确保 getter 如下:
public Office getOffice() {
return office;
}
mappedBy 注释是指属性名称,而不是列名称。此外,由于属性是私有的,hibernate通过getter / setter访问它们,因此你需要它遵循约定(get,Camel Case等)
答案 2 :(得分:2)
mappedBy
的值必须与引用类中的属性名称相同,包括其大小写。
例如,
如果属性的名称为employee
,则必须准确撰写employee
。
如果属性的名称为employeE
,则必须准确撰写employeE
。
(getter function&#39; s name is proper)
答案 3 :(得分:0)
检查您的实体是否都在persistence.xml