Hibernate 是一个很强大的ORM 框架。
仅仅在 one-to-one中就分 单向关联,使用连接表的单向关联,双向关联,使用连接表的双向关联.
这里以 双向关联为主总结一下。
1 关联文件的书写.
com.isw2.entity.UserBO.java 如下:
public class UserBO extends BaseBO { private Long userId; private String userName; private UserInfoBO userInfoBO;
UserBO.hbm.xml如下;
<class name="com.isw2.entity.UserBO" table="t_user" lazy="true"> <id name="userId" column="userId"> <generator class="native"></generator> </id> <property name="userName" column="userName" length="30" type="string"> </property> <!-- 多对一中设置 unique = true <many-to-one name="userInfoBO" cascade="all" column="infoId" unique="true" lazy="proxy"></many-to-one> --> <!-- cascade="all" 使用级联更新,fetch="select" 使用单独的语句查询相关联对象 --> <one-to-one name="userInfoBO" class="com.isw2.entity.UserInfoBO" cascade="all" fetch="select"> </one-to-one> </class>
com.isw2.entity.UserInfoBO.java 如下:
public class UserBO extends BaseBO { private Long userId; private String userName; private UserInfoBO userInfoBO;
UserInfoBO.hbm.xml:
<class name="com.isw2.entity.UserInfoBO" table="t_user_info" lazy="true"> <id name="userId" column="userId"> <generator class="foreign"> <param name="property">UserBO</param> </generator> </id> <property name="userSex" column="userSex" length="2" type="string"> </property> <!-- constrained="true" 添加外键约束 --> <one-to-one name="userBO" constrained="true" class="com.isw2.entity.UserBO" fetch="select" cascade="all"> </one-to-one> </class>
2 相关查询.
/** * 存储对象 * * @param userBO * 要save的对象 */ public void save(UserBO userBO) { Session session = HibernateSessionFactory.getSession(); Transaction tr = session.beginTransaction(); session.save(userBO); tr.commit(); session.close(); } /** * 查找所有对象 */ public void findAll() { Session session = HibernateSessionFactory.getSession(); Transaction tr = session.beginTransaction(); Query query = session.createQuery("from UserBO"); List list = query.list(); for (Object object : list) { System.out.println(object); } tr.commit(); session.close(); } /** * 使用HQL语句查询 */ public void findByHQL() { // 连接查询 String hqlString = "select new UserBO(ub.userId,ub.userName,ib) from UserBO ub,UserInfoBO ib where ub.userId = ib.userId"; Session session = HibernateSessionFactory.getSession(); Transaction tr = session.beginTransaction(); Query query = session.createQuery(hqlString); List list = query.list(); for (Object object : list) { System.out.println(object); } tr.commit(); session.close(); } /** * 使用Criteria 查询 */ public void findByCriteria() { // DetachedCriteria dc = DetachedCriteria.forClass(UserBO.class); // DetachedCriteria 是Criteria的离线版 Session session = HibernateSessionFactory.getSession(); Criteria cr = session.createCriteria(UserBO.class); cr.addOrder(Order.asc("userName")); // cr.setProjection(Projections.max("userId")); 使用聚集函数 // cr.add(Restrictions.gt("userId", new Long(3)));使用Restrictions // Criteria crUserInfo = cr.createCriteria("userInfoBO"); // crUserInfo.add(Restrictions.eq("userSex", "男"));使用多表联接查询 List list = cr.list(); for (Object object : list) { System.out.println(object); } session.close(); } /** * Example查询 */ public void findByExample() { Session session = HibernateSessionFactory.getSession(); UserInfoBO userInfo = new UserInfoBO(); userInfo.setUserSex("男"); Example emample = Example.create(userInfo); Criteria cr = session.createCriteria(UserInfoBO.class); cr.add(emample); List list = cr.list(); for (Object object : list) { UserInfoBO info = (UserInfoBO) object; System.out.println(info.getUserBO().toString()); } session.close(); } /** * Cascade = all 级联更新 */ public void deleteCascade() { Session session = HibernateSessionFactory.getSession(); UserBO userBO = (UserBO) session.load(UserBO.class, new Long(3)); Transaction tr = session.beginTransaction(); session.delete(userBO); tr.commit(); session.close();
3 one-to-one 中关延迟加载.
在当前例子中为又向关联,UserBO主表 , UserInfoBO 从表.
首先 one-to-one 中 fetch="select" 是必须的它表示默认不使用连接查询.
但是因为one-to-one 的特殊性,查询UserBO 过程中无法使用延迟加载,UserInfoBO 中则可以使用.(参考http://ch-space.iteye.com/blog/390780)