【问题标题】:OneToOne in Hibernate - Unknown "mappedBy"休眠中的 OneToOne - 未知的“mappedBy”
【发布时间】:2016-01-13 20:54:18
【问题描述】:

我正在使用 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(我知道这没有多大意义)。

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)

【问题讨论】:

  • 你检查包名是不是“ch.myapp.model.Employee”
  • @srikanthr 嗯,包名是“ch.myapp.model”。嗯,不知道是不是这个问题。

标签: java hibernate hibernate-mapping one-to-one


【解决方案1】:

1.确保属性如下:

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

    public Office getOffice() { return office; }

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

【讨论】:

    【解决方案2】:

    您在 Employee.class 中的 mappedBy 值有误,应该如下:

    @OneToOne(mappedBy = "employee", fetch = FetchType.EAGER)
    private Office office;
    

    【讨论】:

    • @TrudleR 比编辑你的问题,弹出什么错误比 - 他是正确的,这应该有效。此外,粘贴您用于这两个相关属性的吸气剂。两次映射“OFFICECODE”可能有些可疑,一次是 Id,一次不是,但这是另一回事
    【解决方案3】:

    mappedBy 的值必须与被引用类中的属性名称(包括其大小写)相同。

    例如,

    如果属性名称是employee,则必须准确地写成employee

    如果属性名称是employeE,则必须准确地写成employeE

    (getter 函数的名称是正确的)

    【讨论】:

      【解决方案4】:

      检查您的两个实体是否都在persistence.xml

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-13
        • 2011-02-13
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        • 2012-10-14
        相关资源
        最近更新 更多