当我尝试与我的员工和地址实体进行双向一对一映射时,我正在使用hibernate版本5并收到以下错误。
线程“main”中的异常org.hibernate.AnnotationException:未知 mappedBy in:home.practice.Employee.addr,引用的属性unknown: home.practice.Address.employee at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:147) 在 org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1659) 在 org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1634) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) 在 org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) 在 org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at home.practice.mainClass.main(mainClass.java:24)
实体员工
package home.practice;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="EMP")
@SequenceGenerator(name="seq", sequenceName="TAB_CUSTOMER_SEQ", allocationSize=100)
public class Employee implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq")
@Column(name="EMPNO",unique=true,nullable=false,length=6)
private int empno;
@Column(name="ENAME",unique=false,nullable=false,length=20)
private String name;
@OneToOne(mappedBy="employee",cascade={CascadeType.ALL})
private Address addr;
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
地址实体
package home.practice;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name="ADDRESS")
public class Address implements Serializable{
@Column(name="address_line1")
private String line1;
@Column(name="address_line2")
private String line2;
@Column(name="zip")
private long zip;
@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;
@Id
@Column(name="EMPNO",unique=true,nullable=false,length=6)
@GeneratedValue( generator="gen")
@GenericGenerator(name="gen", strategy = "foreign",parameters={ @Parameter(name="property",value="employee")})
private int empno;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public int getAddressId() {
return AddressId;
}
public void setAddressId(int addressId) {
AddressId = addressId;
}
@Id
int AddressId;
public String getLine1() {
return line1;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public String getLine2() {
return line2;
}
public void setLine2(String line2) {
this.line2 = line2;
}
public long getZip() {
return zip;
}
public void setZip(long zip) {
this.zip = zip;
}
}
有人可以看看这个并让我知道它是否是hibernate中的错误或者我的代码中缺少某些内容?我已经在其他线程中尝试了一些相同问题的解决方案,但无济于事。之前在hibernate中存在类似的错误,但在4.2版本中已修复。
答案 0 :(得分:1)
解决了这个问题:
我错过了我的hibernate配置文件:
<mapping class="home.practice.Address"/>
我之前定义过
<mapping class="home.practice.Employee"/>
但不是地址类。