首页 > 系统服务 > 详细

hibernate的数据关联映射

时间:2014-07-22 00:35:05      阅读:444      评论:0      收藏:0      [点我收藏+]

数据关联映射分为:一对多、多对一,一对一,多对多。

cascade属性
• 
save-update:在执行保存和修改是进行级联操作
• 
delete:在执行删除时进行级联操作
• 
all:在所有情况下进行级联操作
 none:不进行级联操作(默认)

抓取策略

 

一对多、多对一

一对多、多对一持久化类设计
// User类
public class User { 
 
private int id; 
private String name; 
private int age; 
private Set<Address> addressSet; 
 
//get set method 
}

//Address类
public class Address { 
 
private int id; 
private String address; 
private int tel; 
private User user; 
 
//get set method 
}
address.hbm.xml
<hibernate-mapping package="com.hussion.pojo"> 
    <class name="Address" table="t_address"> 
        <id name="id"> 
          <generator class="native"/> 
        </id> 
        <property name="address"></property> 
        <property name="tel"></property> 
        <many-to-one name="user" class="User" column="userid"/> 
    </class> 
</hibernate-mapping>
user.hbm.xml
<hibernate-mapping package="com.hussion.pojo">  
    <class name="User" table="t_user"> 
        <id name="id" column="id"> 
          <generator class="native"></generator> 
        </id> 
        <property name="name" column="name"></property> 
        <property name="age" column="age"></property> 

        <!--inverse="true"放弃关系维护--> 
        <set name="addressSet" inverse="true"> 
          <key column="userid"/> 
          <one-to-many class="Address"/> 
        </set> 
    </class>  
</hibernate-mapping>

一对一

持久化类设计
public class Person { 
 
private int id; 
private String userName; 
private Card card; 
 
//get set Method 
} 

public class Card { 
 
private int id; 
private String cardNum; 
private Person person; 
 
//get set Method 
}
person.hbm.xml
<hibernate-mapping package="com.hussion.pojo"> 
    <class name="Person" table="t_person"> 
        <id name="id"> 
          <generator class="native"></generator> 
        </id> 
         <property name="userName"></property> 
          <one-to-one name="card" class="Card" cascade="delete"/>
    </class> 
</hibernate-mapping>

多对多

ublic class Teacher { 
 
private int id; 
private String name; 
private Set<Student> student; 
 
//get set method 
} 
public class Student { 
 
private int id; 
private String name; 
private Set<Teacher> teacher; 
 
//get set method 
}
teacher.hbm.xml
<hibernate-mapping package="com.hussion.pojo"> 
    <class name="Teacher" table="t_teacher"> 
        <id name="id" column="id"> 
          <generator class="native"></generator> 
        </id> 
          <property name="name"></property> 
        <set name="student" table="t_student_techer"> 
          <key column="teacherid"/> 
          <many-to-many class=“Student” column=“studentid”/> 
        </set> 
    </class> 
</hibernate-mapping>
student.hbm.xml
<hibernate-mapping package="com.hussion.pojo"> 
    <class name="Student" table="t_student"> 
        <id name="id" column="id"> 
          <generator class="native"></generator> 
        </id> 
        <property name="name"></property> 
        <set name="teacher" table="t_student_techer"> 
            <key column="studentid"/> 
            <many-to-many class="Teacher" column="teacherid /> 
        </set> 
    </class> 
</hibernate-mapping>

多对多一方要放弃关系维护
<set name="student" table="t_teacher_student"  inverse="true">

排序
<many-to-many class="Student" column="s_id"  order-by="id asc,name desc"></many-to-many>

延迟加载关闭 : lazy="false"

hibernate的数据关联映射,布布扣,bubuko.com

hibernate的数据关联映射

原文:http://www.cnblogs.com/fudapeng/p/3853174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!