0

アプリケーションで非主キーを外部キーとして使用しようとしています。シナリオは次のとおりです。EMPLOYEE および EMPLOYEE_PROPERTIES テーブルがあります。Employee プロパティと Employee プロパティの間には 1 対多の関係があります。ここに私のスキーマがあります:


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

これが私の休止状態のマッピング xml です。

<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;
}

従業員にアクセスしようとすると、次の例外が発生し続けます: 原因: org.hibernate.MappingException: 外部キー (FKF28BCC4680C757C:EMPLOYEE_PROPERTY [emp_id])) には、参照された主キー (EMPLOYEE [FNAME,LNAME] と同じ数の列が必要です) )

非主キーを外部キーとして参照することは可能ですか?

4

1 に答える 1

0

私はあなたのマッピングに混乱しています。Employee から EmployeeProperty は 1 対多ですが、EmployeeProperty の emp_id はマッピングでこのエンティティの ID フィールド (つまり、PK、Unique 列) としてマークされていますか?

さまざまなオプションがあります。

[1] emp_property_id などの ID として機能する別のフィールドを EmployeeProperty に追加します。

[2] Entity ではなく、EmployeeProperty を Embeddable (独自の永続 ID なし) にする

EmployeeProperty が Employee に依存していることを考えると (つまり、EmployeeProperty は Employee から独立して存在することはできません)、おそらく 2 番目のオプションの方が正しいでしょう。

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

于 2013-11-05T08:59:41.087 に答える