首页上一页 1 下一页尾页 5 条记录 1/1页
复合主键 映射set集合
发表在Java图书答疑
2009-02-09
是否精华
是
否
版块置顶:
是
否
hibernate 应用开发完全手册 第9章 映射set
如果company为复合主键(id,companyName),应该如何映射?
一个主键是没有问题的:
<hibernate-mapping>
<class name="com.formBean.Company" table="tb_company">
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<property name="company_name" column="company_name" type="string" not-null="true"/>
<property name="company_type" column="company_type" type="string" not-null="true"/>
<property name="owner" column="owner" type="string" not-null="true"/>
<property name="enrol_fund" column="enrol_fund" type="int" not-null="true"/>
<property name="enrol_date" column="enrol_date" type="date" not-null="true"/>
<set name="depts" table="tb_dept" cascade="all" lazy="false">
<key column="company_id"/>
<element column="name" type="string" not-null="true"/>
</set>
</class>
</hibernate-mapping>
复合主键出错Foreign key (FKA4F77D767347FFCD:tb_dept [company_id])) must have same number of columns
as the referenced primary key (TB_COMPANY2 [ID,COMPANY_NAME]):
<hibernate-mapping>
<class name="com.formBean.TbCompany2" table="TB_COMPANY2" schema="CIM">
<composite-id name="id" class="com.formBean.TbCompany2Id">
<key-property name="id" type="java.math.BigDecimal">
<column name="ID" precision="22" scale="0" />
</key-property>
<key-property name="companyName" type="java.lang.String">
<column name="COMPANY_NAME" length="50" />
</key-property>
</composite-id>
<property name="companyType" type="java.lang.String">
<column name="COMPANY_TYPE" length="50" />
</property>
<property name="owner" type="java.lang.String">
<column name="OWNER" length="50" />
</property>
<property name="enrolFund" type="java.math.BigDecimal">
<column name="ENROL_FUND" precision="22" scale="0" />
</property>
<property name="enrolDate" type="java.sql.Timestamp">
<column name="ENROL_DATE" length="7" />
</property>
<set name="depts" table="tb_dept" cascade="all" lazy="false">
<key column="company_id"/>
<element column="name" type="string" not-null="true"/>
</set>
</class>
如果company为复合主键(id,companyName),应该如何映射?
一个主键是没有问题的:
<hibernate-mapping>
<class name="com.formBean.Company" table="tb_company">
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<property name="company_name" column="company_name" type="string" not-null="true"/>
<property name="company_type" column="company_type" type="string" not-null="true"/>
<property name="owner" column="owner" type="string" not-null="true"/>
<property name="enrol_fund" column="enrol_fund" type="int" not-null="true"/>
<property name="enrol_date" column="enrol_date" type="date" not-null="true"/>
<set name="depts" table="tb_dept" cascade="all" lazy="false">
<key column="company_id"/>
<element column="name" type="string" not-null="true"/>
</set>
</class>
</hibernate-mapping>
复合主键出错Foreign key (FKA4F77D767347FFCD:tb_dept [company_id])) must have same number of columns
as the referenced primary key (TB_COMPANY2 [ID,COMPANY_NAME]):
<hibernate-mapping>
<class name="com.formBean.TbCompany2" table="TB_COMPANY2" schema="CIM">
<composite-id name="id" class="com.formBean.TbCompany2Id">
<key-property name="id" type="java.math.BigDecimal">
<column name="ID" precision="22" scale="0" />
</key-property>
<key-property name="companyName" type="java.lang.String">
<column name="COMPANY_NAME" length="50" />
</key-property>
</composite-id>
<property name="companyType" type="java.lang.String">
<column name="COMPANY_TYPE" length="50" />
</property>
<property name="owner" type="java.lang.String">
<column name="OWNER" length="50" />
</property>
<property name="enrolFund" type="java.math.BigDecimal">
<column name="ENROL_FUND" precision="22" scale="0" />
</property>
<property name="enrolDate" type="java.sql.Timestamp">
<column name="ENROL_DATE" length="7" />
</property>
<set name="depts" table="tb_dept" cascade="all" lazy="false">
<key column="company_id"/>
<element column="name" type="string" not-null="true"/>
</set>
</class>