Hibernate多外键问题

hibernate 多个外键配置

近来做的一个项目中用到了一个表出现了多个外键,本想在hbm.xml文件中配置多个<many-to-one>问题就可以解决了,没想到不行。网上search了一个晚上,终于有了一点灵感。

CREATE TABLE `work` (
  `Id` int(11) NOT NULL auto_increment,
  `work_type` int(11) default NULL,    --著作类型
  `author_type` int(11) default NULL,   --作者排序
  `subject_type` int(11) default NULL,   --学科分类
  PRIMARY KEY  (`Id`),
) ;

--字段对应表
CREATE TABLE `work_field` (
  `Id` int(11) NOT NULL auto_increment,   --编号
  `field_name` varchar(255) default NULL,   --代表的意义
  PRIMARY KEY  (`Id`)
) ;

下面是domain类以及相应的配置文件

//著作类
public class Work implements java.io.Serializable {       

        private Integer id;  //自增主键
       
        private WorkField workFieldBySubjectType;  //著作类型
       
        private WorkField workFieldByAuthorType;  //作者排序
       
        private WorkField workFieldByWorkType;  //学科分类

        //其它为getter和setter方法
}

//字段对应类
public class WorkField implements java.io.Serializable {       

        private Integer id;  //编号

        private String fieldName;  //代表的意义

        private Set worksForSubjectType = new HashSet(0);

        private Set worksForWorkType = new HashSet(0);

        private Set worksForAuthorType = new HashSet(0);

        //其它为getter和setter方法

}       

这是Work.hbm.xml中的配置文件
<hibernate-mapping>
    <class name="com.oristand.demo.Work" table="work" catalog="hibernate">
        <id name="id" type="java.lang.Integer">
            <column name="Id" />
            <generator class="identity"></generator>
        </id>
        <many-to-one name="workFieldBySubjectType" class="com.oristand.demo.WorkField" fetch="select">
            <column name="subject_type" />
        </many-to-one>
        <many-to-one name="workFieldByAuthorType" class="com.oristand.demo.WorkField" fetch="select">
            <column name="author_type" />
        </many-to-one>
        <many-to-one name="workFieldByWorkType" class="com.oristand.demo.WorkField" fetch="select">
            <column name="work_type" />
        </many-to-one>
        <property name="remark" type="java.lang.String">
            <column name="remark" />
        </property>
    </class>
</hibernate-mapping>

这是WorkField.hbm.xml中的配置文件
<hibernate-mapping>
    <class name="com.oristand.demo.WorkField" table="work_field" catalog="hibernate">
        <id name="id" type="java.lang.Integer">
            <column name="Id" />
            <generator class="identity"></generator>
        </id>
        <property name="fieldName" type="java.lang.String">
            <column name="field_name" />
        </property>
        <set name="worksForSubjectType" inverse="true">
            <key>
                <column name="subject_type" />
            </key>
            <one-to-many class="com.oristand.demo.Work" />
        </set>
        <set name="worksForWorkType" inverse="true">
            <key>
                <column name="work_type" />
            </key>
            <one-to-many class="com.oristand.demo.Work" />
        </set>
        <set name="worksForAuthorType" inverse="true">
            <key>
                <column name="author_type" />
            </key>
            <one-to-many class="com.oristand.demo.Work" />
        </set>
    </class>
</hibernate-mapping>

你可能感兴趣的:(Hibernate,xml)