gpt4 book ai didi

java - hibernate 中的 OneToOne - 未知 "mappedBy"

转载 作者:行者123 更新时间:2023-11-29 09:40:37 24 4
gpt4 key购买 nike

我正在使用 hibernate 功能进行锻炼,但几个小时以来我一直未能实现 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 和 Employee 之间实现 1:1(我知道这没有多大意义)。

员工类

@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)

最佳答案

1.确保属性如下:

@OneToOne(mappedBy = "employee", fetch = FetchType.EAGER)
private Office office;
  1. 确保getter如下:

    公共(public)办公室 getOffice() {
    返回办公室;
    }

ma​​ppedBy 注释是指属性名称,而不是列名称。此外,由于属性是私有(private)的,hibernate 通过它们的 getter/setter 访问它们,因此您需要它遵循约定(get、Camel Case 等)

关于java - hibernate 中的 OneToOne - 未知 "mappedBy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33147853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com