java Hibernate 中的 OneToOne - 未知的“mappedBy”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33147853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OneToOne in Hibernate - Unknown "mappedBy"
提问by codepleb
I'm exercising with hibernate and I'm failing since hours in implementing a OneToOne relationship. I read a lot of answers on stackoverflow and went through 2 tutorials (mkyong and some other guy), but I don't get what I'm doing wrong.
我正在使用 hibernate 进行锻炼,并且在实施 OneToOne 关系方面失败了几个小时。我在 stackoverflow 上阅读了很多答案并浏览了 2 个教程(mkyong 和其他一些人),但我不明白我做错了什么。
I tried a lot of things, but I always end up with the same exception (org.hibernate.AnnotationException: Unknown mappedBy in: ch.myapp.model.Employee.office, referenced property unknown: ch.myapp.model.Office.EMPLOYEES
)
我尝试了很多东西,但最终总是出现相同的异常 ( org.hibernate.AnnotationException: Unknown mappedBy in: ch.myapp.model.Employee.office, referenced property unknown: ch.myapp.model.Office.EMPLOYEES
)
I would be really glad if someone could give me a hint where the problem lies.
如果有人能给我一个问题所在的提示,我会很高兴。
I'm using this database schemeand I try to implement a 1:1 between Office and Employee (I know that this doesn't make a lot of sense).
我正在使用这个数据库方案,并尝试在 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
办公室.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:
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):
这是我得到的 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)
回答by javaguest
1.Make sure the attributeis as follows:
1.确定属性如下:
@OneToOne(mappedBy = "employee", fetch = FetchType.EAGER)
private Office office;
Make sure the getteris as follows:
public Office getOffice() { return office; }
确保getter如下所示:
public Office getOffice() { return office; }
mappedByannotations refers to attribute names, not column names. Also since attributes are private, hibernate access them through their getter/setter, thus you need it to follow convention (get,Camel Case, etc)
mappingBy注释是指属性名称,而不是列名称。此外,由于属性是私有的,hibernate 通过它们的 getter/setter 访问它们,因此您需要它遵循约定(get、Camel Case 等)
回答by Joung hee Park
the value of mappedBy
must be same as the attribute's name in referenced class including its case.
的值mappedBy
必须与引用类中的属性名称相同,包括其大小写。
for instance,
例如,
If the attribute's name is employee
, you have to write exactly employee
.
如果属性的名称是employee
,则必须准确地写出employee
。
If the attribute's name is employeE
, you have to write exactly employeE
.
如果属性的名称是employeE
,则必须准确地写出employeE
。
(getter function's name is proper)
(getter 函数的名称是正确的)
回答by pezetem
you had wrong mappedBy value in Employee.class it should be the following:
您在 Employee.class 中有错误的 mappingBy 值,它应该是以下内容:
@OneToOne(mappedBy = "employee", fetch = FetchType.EAGER)
private Office office;
回答by Joren Inghelbrecht
Check if both your entities are in persistence.xml
检查您的两个实体是否都在 persistence.xml