1.
模型
多个用户(user)对应一个组(group)
2.
实体类(省略set,get方法)
public class User {
private int id;
private String name;
private Group group;
}
public class Group {
private int id;
private String name;
}
3.
数据模型
mysql> desc t_user;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| relatedGroup | int(11) | YES | MUL | NULL | |
+--------------+--------------+------+-----+---------+----------------+
mysql> desc t_group;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
4.
映射文件
<many-to-one>标签会在“多”的一端添加一个外键,指向“一”的一端,这个外键是由<many-to-one>标签中的column属性定义的。
<many-to-one>标签的定义实例:
<many-to-one name="group" column="relatedGroup" />
可以指定class属性,如果不指定Hibernate会自动找其关联的实体类。如User实体中有Group属性值group,所以Hibernate可以知道User实体相关联的实体类
为Group。且<many-to-one>中的name值需和实体类中的关联属性相同,才能够通过反射找到相应的关联实体类。
User.hbm.xml:
<hibernate-mapping>
<class name="com.dvnchina.hibernate.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="group" column="relatedGroup" />
</class>
</hibernate-mapping>
Group.hbm.xml:
<hibernate-mapping>
<class name="com.dvnchina.hibernate.Group" table="t_group">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
5.
测试
public class TestManyToOne extends TestCase {
public void testSave(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
Group group = new Group();
group.setName("凤山人在线");
User user1 = new User();
User user2 = new User();
user1.setName("yang9");
user1.setGroup(group);
user2.setName("long9");
user2.setGroup(group);
//session.save(group);
//当group没有save,即为Transient对象,将抛出TransientObjectException
//因为持久对象不能直接引用瞬时对象,所以我们需要先save(group)使其为持久对象后
//再引用
session.save(user1);
session.save(user2);
tx.commit();
HibernateUtils.closeSession(session);
}
public void testCascade(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
Group group = new Group();
group.setName("天峨人在线");
User user1 = new User();
User user2 = new User();
user1.setName("yang999");
user1.setGroup(group);
user2.setName("long999");
user2.setGroup(group);
//在user.hbm.xml中的many-to-one中设置级联设置cascade="all",此时不需要使group
//为持久对象而可以直接使用它,因为save(user1oruser2)时将级联保存group
session.save(user1);
session.save(user2);
tx.commit();
HibernateUtils.closeSession(session);
}
//级联是对象之间的连锁操作(只对增删改起作用,对load不起作用,load是由many-to-one维护的)
public void testLoad(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
User user = (User)session.load(User.class, 3);
System.out.println(user.getName()+","+user.getGroup().getName());
tx.commit();
HibernateUtils.closeSession(session);
}
}
模型
多个用户(user)对应一个组(group)
2.
实体类(省略set,get方法)
public class User {
private int id;
private String name;
private Group group;
}
public class Group {
private int id;
private String name;
}
3.
数据模型
mysql> desc t_user;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| relatedGroup | int(11) | YES | MUL | NULL | |
+--------------+--------------+------+-----+---------+----------------+
mysql> desc t_group;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
4.
映射文件
<many-to-one>标签会在“多”的一端添加一个外键,指向“一”的一端,这个外键是由<many-to-one>标签中的column属性定义的。
<many-to-one>标签的定义实例:
<many-to-one name="group" column="relatedGroup" />
可以指定class属性,如果不指定Hibernate会自动找其关联的实体类。如User实体中有Group属性值group,所以Hibernate可以知道User实体相关联的实体类
为Group。且<many-to-one>中的name值需和实体类中的关联属性相同,才能够通过反射找到相应的关联实体类。
User.hbm.xml:
<hibernate-mapping>
<class name="com.dvnchina.hibernate.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="group" column="relatedGroup" />
</class>
</hibernate-mapping>
Group.hbm.xml:
<hibernate-mapping>
<class name="com.dvnchina.hibernate.Group" table="t_group">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
5.
测试
public class TestManyToOne extends TestCase {
public void testSave(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
Group group = new Group();
group.setName("凤山人在线");
User user1 = new User();
User user2 = new User();
user1.setName("yang9");
user1.setGroup(group);
user2.setName("long9");
user2.setGroup(group);
//session.save(group);
//当group没有save,即为Transient对象,将抛出TransientObjectException
//因为持久对象不能直接引用瞬时对象,所以我们需要先save(group)使其为持久对象后
//再引用
session.save(user1);
session.save(user2);
tx.commit();
HibernateUtils.closeSession(session);
}
public void testCascade(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
Group group = new Group();
group.setName("天峨人在线");
User user1 = new User();
User user2 = new User();
user1.setName("yang999");
user1.setGroup(group);
user2.setName("long999");
user2.setGroup(group);
//在user.hbm.xml中的many-to-one中设置级联设置cascade="all",此时不需要使group
//为持久对象而可以直接使用它,因为save(user1oruser2)时将级联保存group
session.save(user1);
session.save(user2);
tx.commit();
HibernateUtils.closeSession(session);
}
//级联是对象之间的连锁操作(只对增删改起作用,对load不起作用,load是由many-to-one维护的)
public void testLoad(){
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
User user = (User)session.load(User.class, 3);
System.out.println(user.getName()+","+user.getGroup().getName());
tx.commit();
HibernateUtils.closeSession(session);
}
}
本文转自 yzzh9 51CTO博客,原文链接:http://blog.51cto.com/java999/119431,如需转载请自行联系原作者