hibernate中对象-关联关系分为三种:
1.one-to-one
2.one-to-many/many-to-one
3.many-to-many
其中one-to-many是最主要的一种关联关系,另外两种关联关系可以通过one-to-many或者many-to-one进行扩展.
先来看one-to-one:
User持久化类:
public class User { private Integer u_id; private String userName; //用户对应多个订单,这其中就是一对多得关联关系 private Set<Order> order = new HashSet<Order>(); public Integer getU_id() { return u_id; } public void setU_id(Integer u_id) { this.u_id = u_id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
Order持久化类:
public class Order { private int orderNum; private Integer o_id; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public Integer getO_id() { return o_id; } public void setO_id(Integer o_id) { this.o_id = o_id; } }
在来看看配置文件:
<hibernate-mapping package="com.lovo"> <class name="User" table="t_user"> <id name="u_id" column="f_user_id" type="integer"> <generator class="identity"></generator> </id>
<!--这里省略了User,Order类基本属性的映射-->
<!--order是User类中Set集合属性对应字段名--> <set name="order" cascade="delete" inverse="true"> <!-- 通过订单表定的字段fk_user_id来关联,外键,是字段名,设置级联为delete,保证用户删除同时删除用户订单 --> <!--inverse="true"将一对多关系中"一"方的维护交给"多方",是一对多关联关系中重要的设置-->
<key column="fk_user_id"> </key> <!-- 设置一对多的关系 --> <one-to-many class="Order" /> </set>
Order类的映射配置:
<class name="Order" table="t_order"> <id name="o_id" type="integer" column="f_o_id"> <generator class="identity"></generator> </id> <!--建立与User的多对一关联关系,通过一个外键去关联User类 --> <many-to-one name="user" class="User" column="fk_user_id"> </many-to-one>
用main方法来先建表:
public class CreateTable { public static void main(String[] args) { Configuration cfg=new Configuration().configure(); SchemaExport se=new SchemaExport(cfg); se.create(false, true); } }
写的一个得到session的单例类
public class HibernateUtil { private static SessionFactory sf; private static final ThreadLocal<Session> threadlocal = new ThreadLocal<Session>(); static { try{ //// Create the SessionFactory from hibernate.cfg.xml Configuration cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { Session session = (Session) threadlocal.get(); if (session == null) { session = sf.openSession(); threadlocal.set(session); } return session; } public static void closeSession() throws HibernateException { Session session = (Session) threadlocal.get(); threadlocal.set(null); if (session != null) { session.close(); } } }
用JUnit做一个保存方法的单元测试
public class TestUser extends TestCase { public void testSave() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); User user=new User(); user.setUserName("fisher"); Order order =new Order(); Order order1=new Order(); order.setOrderNum("100011"); order1.setOrderNum("100012"); //建立双向关联关系,user类中一对多关系,Order类中多对一关系 user.getOrder().add(order); order1.setUser(user);
order.setUser(user); tx.commit(); HibernateUtil.closeSession();
}
}