简体   繁体   English

Hibernate中的OneToOne-未知的“ mappedBy”

[英]OneToOne in Hibernate - Unknown “mappedBy”

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. 我在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相同的异常( 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 scheme and 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 办公室类

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

you had wrong mappedBy value in Employee.class it should be the following: 您在Employee.class中的mapBy值错误,应为以下内容:

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

1.Make sure the attribute is as follows: 1.确保属性如下:

@OneToOne(mappedBy = "employee", fetch = FetchType.EAGER)
private Office office;
  1. Make sure the getter is as follows: 确保吸气剂如下:

    public Office getOffice() { return office; }

mappedBy annotations refers to attribute names, not column names. mappingBy批注引用属性名称,而不是列名称。 Also since attributes are private, hibernate access them through their getter/setter, thus you need it to follow convention (get,Camel Case, etc) 另外,由于属性是私有的,所以休眠通过其getter / setter访问它们,因此您需要遵循约定(get,Camel Case等)。

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函数的名称正确)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM