你好,游客 登录 注册 搜索
背景:
阅读新闻

Hibernate 是如何管理数据库表的关联关系的

[日期:2012-05-31] 来源:Linux社区  作者:walkingmanc [字体:大 中 小]

数据库的关联关系是通过主外键实现的: 

Hibernate中管理的实体类的关联关系是通过包含来实现的,根据关联的2方是否相互包含分为单向关联和双向关联:

Hibernate 配置实体关联不仅仅要在实体类中包含关联类,而且要在hbm映射文件中说明。 

 

 

一对一外键关联 

Class Team()

{

Int teamid;

String teamname;

Address address;

 

}

 

<hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.Team" table="team">
        <id name="id" type="integer">
            <column name="id"></column>
            <generator class="increment"></generator>
        </id>

        <property name="name" column="name" type="string"></property>
        
        <many-to-one name="adress" class="com.suxiaolei.hibernate.pojos.Adress" column="adress_id" unique="true"></many-to-one>

    </class>
</hibernate-mapping>

 

Class Address()

{

Int addId;

String city;

String detailAdd;

 

Team team;

} 

 

<hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.Adress" table="adress">
        <id name="id" type="integer">
            <column name="id"></column>
            <generator class="increment"></generator>
        </id>

        <property name="city" column="city" type="string"></property>
        
        <one-to-one name="team" class="com.suxiaolei.hibernate.pojos.Team" cascade="all"></one-to-one>

    </class>

  

一对一主键关联

Class Team()

{

Int teamid;

String teamname;

Address address;

 

}

 

 

 

<hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.Team" table="team">
        <id name="id" type="integer">
            <column name="id"></column>
            <generator class="foreign">
                <param name="property">adress</param>
            </generator>
        </id>

        <property name="name" column="name" type="string"></property>
        
        <one-to-one name="adress" class="com.suxiaolei.hibernate.pojos.Adress" cascade="all"></one-to-one>

    </class>
</hibernate-mapping>

 

 

 

Class Address()

{

Int addId;

String city;

String detailAdd;

 

Team team;

}

 
<class name="com.suxiaolei.hibernate.pojos.Adress" table="adress">
        <id name="id" type="integer">
            <column name="id"></column>
            <generator class="increment"></generator>
        </id>

        <property name="city" column="city" type="string"></property>
        
        <one-to-one name="team" class="com.suxiaolei.hibernate.pojos.Team" cascade="all"></one-to-one>

    </class>

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款