User和Group,关系多对一。
注意:cascade是级联。也就是说对主表保存、更新、删除都会级联的操作附表。
User.java
1 public class User {
private String id;
3 private String name;
4 private int age;
5 private Group group;
6 get(),set()省略
7 }
Group.java
1 public class Group {
2 private String id ;
3 private String name ;
...get(),set()省略
4 }
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zzn.hibernate.many2One.User" table="USER">
<id name="id" column="id">
<generator class="uuid" />
</id>
<property name="name" />
<property name="age" />
<many-to-one name="group" column="GROUP_ID" cascade="all"/>
</class>
</hibernate-mapping>
Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zzn.hibernate.many2One.Group" table="T_GROUP">
<id name="id" column="id">
<generator class="uuid" />
</id>
<property name="name" />
</class>
</hibernate-mapping>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/zzn/hibernate/many2One/User.hbm.xml"/>
<mapping resource="com/zzn/hibernate/many2One/Group.hbm.xml"/>
</session-factory>
</hibernate-configuration>
test.java
package com.zzn.hibernate;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zzn.hibernate.hibernateUtil.HibernateUtils;
import com.zzn.hibernate.many2One.Group;
import com.zzn.hibernate.many2One.User;
public class Many2OneTest extends TestCase {
public void testSaveMany2One(){
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtils.getSession();
transaction= session.beginTransaction();
Group group = new Group();
group.setName("11111");
User user = new User();
user.setName("刘德华");
user.setAge(80);
user.setGroup(group);
User user2 = new User();
user2.setName("周杰伦");
user2.setAge(25);
user2.setGroup(group);
session.save(user1);
session.save(user2);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
public void testUpdateMany2One(){
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtils.getSession();
transaction= session.beginTransaction();
User user = (User)session.get(User.class, "402881e424a9fb110124a9fb124c0001");
Group group = new Group();
group.setName("无敌");
user.setName("刘天王");
user.setAge(22);
user.setGroup(group);
session.update(user);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
public void testDeleteMany2One(){
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtils.getSession();
transaction= session.beginTransaction();
User user = (User)session.get(User.class, "402881e424a9e7d80124a9e7d9270001");
session.delete(user);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
}