本文主要是介绍Hibernate 双向ManyToMany 究极解决 新增-删除-修改-查询(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DAO设计:
已经设计好数据实体以及关联关系了,接下来DAO层的编码,这部分没什么东西,可以直接无视
HibernateUtil:
package org.taomujian.db;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final SessionFactory sessionFactory = buildSessionFactory();private static SessionFactory buildSessionFactory() {try {// Create the SessionFactory from hibernate.cfg.xmlreturn new Configuration().configure().buildSessionFactory();}catch (Throwable ex) {// Make sure you log the exception, as it might be swallowedSystem.err.println("Initial SessionFactory creation failed." + ex);throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {return sessionFactory;}}
OrgDAO:
package org.taomujian.dao;import org.hibernate.Session;
import org.taomujian.db.HibernateUtil;
import org.taomujian.model.Org;public class OrgDAO{public void saveOrg(Org org){if(null==org) return ;Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();session.persist(org);session.getTransaction().commit();}public void delOrg(Org org){Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();session.delete(org);session.getTransaction().commit();}public void mergeOrg(Org org){if(null==org) return ;Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();session.merge(org);session.getTransaction().commit();}public Org getOrg(String id){Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();Org org = (Org)session.get(Org.class, id);session.getTransaction().commit();return org;}
}
RoleDAO:
package org.taomujian.dao;import org.hibernate.Session;
import org.taomujian.db.HibernateUtil;
import org.taomujian.model.Role;public class RoleDAO {/*** 保存role记录,可以只单独保存role,也可以完整的保存role以及org和级联关系* * @param role*/public void saveRole(Role role){if(null==role) return ;Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();session.persist(role);session.getTransaction().commit();}public void mergeRole(Role role){if(null==role) return ;Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();session.merge(role);session.getTransaction().commit();}/*** 根据角色的ID获取一条role数据* @param id* @return*/public Role getRole(String id){Session session = HibernateUtil.getSessionFactory().getCurrentSession();session.beginTransaction();Role role = (Role)session.get(Role.class, id);System.out.println(role.getOrgList());session.getTransaction().commit();return role;}/*** 删除角色:* @param id*/public void removeRole(Role role){Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.delete(role);session.getTransaction().commit(); }}
这篇关于Hibernate 双向ManyToMany 究极解决 新增-删除-修改-查询(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!