hibernate外键映射到唯一键

时间:2013-11-04 21:56:05

标签: java hibernate

我正在尝试将非主键作为我的应用程序中的外键。这是场景: 我有EMPLOYEE和EMPLOYEE_PROPERTIES表。 Employee和Employee属性之间存在一对多关系。这是我的架构:


create table employee(
 fname varchar2(100) not null,
 lname varchar2(100) not null,
 emp_id number not null
);
ALTER TABLE employee ADD constraint employee_pk PRIMARY KEY (fname, lname);
alter table employee add constraint employee_uniue unique (emp_id);

create table employee_property(
 emp_prop_id not null,
 emp_id number not null,
 property_name varchar2(100) not null,
 property_value varchar2(100) not null
);
ALTER TABLE employee_property ADD constraint employee_property_pk PRIMARY KEY (emp_prop_id);
ALTER TABLE employee_property ADD CONSTRAINT emp_prop_fk FOREIGN KEY (emp_id) REFERENCES employee(emp_id);

这是我的hibernate映射xmls: -----------------雇员------------------------

<hibernate-mapping>
    <class name="com.persistence.vo.Employee" table="EMPLOYEE">
        <composite-id>
            <key-property name="fName" column="FNAME"/>
            <key-property name="lName" column="LNAME"/>
        </composite-id>
        <property name="empId" type="long" access="field" unique="true">
            <column name="EMP_ID" />
        </property>
        <set name="employeeProperties" table="employee_properties" lazy="false"
            fetch="select" cascade="save-update, delete-orphan">
            <key>
                <column name="emp_id" not-null="true"/>
            </key>
            <one-to-many entity-name="com.persistence.vo.EmployeeProperty"/>
        </set>
    </class>
</hibernate-mapping>

-------------------员工属性---------------------

<hibernate-mapping>
    <class name="com.persistence.vo.EmployeeProperty" table="EMPLOYEE_PROPERTY">
        <id name="empPropId" type="long">
            <column name="EMP_PROP_ID" />
            <generator class="assigned" />
        </id>
    <many-to-one name="employee" class="com.persistence.vo.Employee" fetch="select">
        <column name="empId" not-null="true"/>
    </many-to-one>
        <property name="propertyName" type="java.lang.String">
            <column name="PROPERTY_NAME" />
        </property>
        <property name="propertyValue" type="java.lang.String">
            <column name="PROPERTY_VALUE" />
        </property>
    </class>
</hibernate-mapping>

----------- Employee.java -------------------------

public class Employee {

    private long empId;
    private String fName;
    private String lName;

    private Set<EmployeeProperty> employeeProperties;
}

------------ EmployeeProperty.java -------------------

public class EmployeeProperty {

    private long empPropId;
    private Employee employee;
    private String propertyName;
    private String propertyValue;
}

当我尝试访问Employee时,我一直遇到以下异常: 引起:org.hibernate.MappingException:外键(FKF28BCC4680C757C:EMPLOYEE_PROPERTY [emp_id]))必须与引用的主键具有相同的列数(EMPLOYEE [FNAME,LNAME])

是否可以将非主键作为外键引用?

1 个答案:

答案 0 :(得分:0)

我对你的映射感到困惑。员工到EmployeeProperty是一对多,但EmployeeProperty中的emp_id在您的映射中标记为此实体的ID字段(即PK,唯一列)?

您有多种选择:

[1]将另一个字段添加到EmployeeProperty以充当ID,例如emp_property_id

[2]使EmployeeProperty成为一个可嵌入的(没有自己的持久性身份)而不是一个实体

鉴于EmployeeProperty依赖于Employee(即EmployeeProperty不能独立于Employee存在),第二个选项可能更正确。

http://en.wikibooks.org/wiki/Java_Persistence/Embeddables