多对多双向关联

 
  1. 一个简单示例:   
  2. 一个雇员可以担任几个角色,一个角色可以拥有多个雇员,雇员和角色之间存在多对多的关联关系,   
  3. 在这里我们建立多对多的双向关联...   
  4.   
  5.   
  6.   
  7. db schema:   
  8. ==============================   
  9.   
  10.   
  11. 雇员表:   
  12.   
  13. CREATE TABLE [employee] (   
  14.     [eid] [int] IDENTITY (11) NOT NULL ,   
  15.     [ename] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,   
  16.     [join_date] [datetime] NOT NULL ,   
  17.      PRIMARY KEY  CLUSTERED    
  18.     (   
  19.         [eid]   
  20.     )  ON [PRIMARY]    
  21. ) ON [PRIMARY]   
  22. GO   
  23.   
  24.   
  25.   
  26. 角色表:   
  27.   
  28. CREATE TABLE [role] (   
  29.     [roleid] [int] IDENTITY (11) NOT NULL ,   
  30.     [rolename] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,   
  31.      PRIMARY KEY  CLUSTERED    
  32.     (   
  33.         [roleid]   
  34.     )  ON [PRIMARY]    
  35. ) ON [PRIMARY]   
  36. GO   
  37.   
  38.   
  39. 雇员角色表:   
  40.   
  41. CREATE TABLE [emp_role] (   
  42.     [er_id] [int] IDENTITY (11) NOT NULL ,   
  43.     [er_eid] [int] NULL ,   
  44.     [er_roleid] [int] NULL ,   
  45.      PRIMARY KEY  CLUSTERED    
  46.     (   
  47.         [er_id]   
  48.     )  ON [PRIMARY] ,   
  49.      FOREIGN KEY    
  50.     (   
  51.         [er_eid]   
  52.     ) REFERENCES [employee] (   
  53.         [eid]   
  54.     ),   
  55.      FOREIGN KEY    
  56.     (   
  57.         [er_roleid]   
  58.     ) REFERENCES [role] (   
  59.         [roleid]   
  60.     )   
  61. ) ON [PRIMARY]   
  62. GO   
  63.   
  64. Employee.java   
  65. --------------------------   
  66. package com.supersit.hibernate.bean;   
  67.   
  68. import java.util.Date;   
  69. import java.util.Set;   
  70.   
  71. public class Employee implements java.io.Serializable,   
  72.         com.supersit.hibernate.Interface.ObjectIn {   
  73.   
  74.     /**  
  75.      *   
  76.      */  
  77.     private static final long serialVersionUID = 1L;   
  78.   
  79.        
  80.     //雇员编号   
  81.     private Integer eid;   
  82.   
  83.     //雇员姓名   
  84.     private String ename;   
  85.   
  86.     //入职时间   
  87.     private java.util.Date join_date;   
  88.   
  89.        
  90.     //角色集合   
  91.     private java.util.Set<Role> roles = new java.util.HashSet<Role>();   
  92.   
  93.     public Employee(Integer eid, String ename, Date join_date) {   
  94.         this.eid = eid;   
  95.         this.ename = ename;   
  96.         this.join_date = join_date;   
  97.     }   
  98.   
  99.     public Employee(Integer eid, String ename, Date join_date, Set<Role> roles) {   
  100.         this.eid = eid;   
  101.         this.ename = ename;   
  102.         this.join_date = join_date;   
  103.         this.roles = roles;   
  104.     }   
  105.   
  106.     public java.util.Set getRoles() {   
  107.         return roles;   
  108.     }   
  109.   
  110.     public void setRoles(java.util.Set<Role> roles) {   
  111.         this.roles = roles;   
  112.     }   
  113.   
  114.     public Employee() {   
  115.     }   
  116.   
  117.     public Integer getEid() {   
  118.         return eid;   
  119.     }   
  120.   
  121.     public void setEid(Integer eid) {   
  122.         this.eid = eid;   
  123.     }   
  124.   
  125.     public String getEname() {   
  126.         return ename;   
  127.     }   
  128.   
  129.     public void setEname(String ename) {   
  130.         this.ename = ename;   
  131.     }   
  132.   
  133.     public java.util.Date getJoin_date() {   
  134.         return join_date;   
  135.     }   
  136.   
  137.     public void setJoin_date(java.util.Date join_date) {   
  138.         this.join_date = join_date;   
  139.     }   
  140.   
  141. }   
  142.   
  143.   
  144.   
  145.   
  146. Role.java   
  147. ------------------------------------   
  148. package com.supersit.hibernate.bean;   
  149.   
  150. import java.util.Set;   
  151.   
  152. public class Role implements java.io.Serializable,   
  153.         com.supersit.hibernate.Interface.ObjectIn {   
  154.   
  155.        
  156.     //角色编号   
  157.     private Integer roleid;   
  158.   
  159.        
  160.     //角色名称   
  161.     private String rolename;   
  162.   
  163.        
  164.     //此角色下的雇员集合   
  165.     private java.util.Set emps = new java.util.HashSet();   
  166.   
  167.     public Role(Integer roleid, String rolename, Set emps) {   
  168.         this.roleid = roleid;   
  169.         this.rolename = rolename;   
  170.         this.emps = emps;   
  171.     }   
  172.   
  173.     public java.util.Set getEmps() {   
  174.         return emps;   
  175.     }   
  176.   
  177.     public void setEmps(java.util.Set emps) {   
  178.         this.emps = emps;   
  179.     }   
  180.   
  181.     public Role() {   
  182.     }   
  183.   
  184.     public Integer getRoleid() {   
  185.         return roleid;   
  186.     }   
  187.   
  188.     public Role(Integer roleid, String rolename) {   
  189.         this.roleid = roleid;   
  190.         this.rolename = rolename;   
  191.     }   
  192.   
  193.     public void setRoleid(Integer roleid) {   
  194.         this.roleid = roleid;   
  195.     }   
  196.   
  197.     public String getRolename() {   
  198.         return rolename;   
  199.     }   
  200.   
  201.     public void setRolename(String rolename) {   
  202.         this.rolename = rolename;   
  203.     }   
  204.   
  205. }   
  206.   
  207.   
  208.   
  209. 配置文件:   
  210.   
  211. hibernate.cfg.xml   
  212. ------------------------------   
  213. <?xml version='1.0' encoding='UTF-8'?>   
  214. <!DOCTYPE hibernate-configuration PUBLIC   
  215.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  216.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  217.   
  218. <!-- Generated by MyEclipse Hibernate Tools.                   -->   
  219. <hibernate-configuration>   
  220.   
  221. <session-factory>   
  222.     <property name="connection.username">sa</property>   
  223.     <property name="connection.url">   
  224.         jdbc:microsoft:sqlserver://localhost:1433;databasename=hibernate   
  225.     </property>   
  226.     <property name="dialect">   
  227.         org.hibernate.dialect.SQLServerDialect   
  228.     </property>   
  229.     <property name="connection.password">sa</property>   
  230.     <property name="connection.driver_class">   
  231.         com.microsoft.jdbc.sqlserver.SQLServerDriver   
  232.     </property>   
  233.     <property name="show_sql">true</property>   
  234.     <mapping resource="com/supersit/hibernate/mapping/Employee.hbm.xml" />   
  235.     <mapping resource="com/supersit/hibernate/mapping/Role.hbm.xml" />   
  236.   
  237. </session-factory>   
  238.   
  239. </hibernate-configuration>   
  240.   
  241.   
  242.   
  243. 映射文件:   
  244.   
  245. Employee.hbm.xml   
  246. -----------------------------   
  247. <?xml version="1.0" encoding="UTF-8"?>   
  248. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >   
  249. <hibernate-mapping package="com.supersit.hibernate.bean">   
  250.     <class name="Employee" table="employee" schema="dbo"  
  251.         catalog="hibernate">   
  252.         <id name="eid" column="eid" type="int">   
  253.             <generator class="identity"></generator>   
  254.         </id>   
  255.         <property name="ename" column="ename" type="string"  
  256.             not-null="true">   
  257.         </property>   
  258.         <property name="join_date" column="join_date" type="date"></property>   
  259.   
  260.         <!-- 映射roles属性,和连接表emp_role进行关联,级联方式:保存更新,检索方式:立即检索 -->   
  261.         <set name="roles" cascade="save-update" lazy="false"  
  262.             table="emp_role">   
  263.   
  264.             <!-- 连接表的字段er_eid作为外键依赖于employee表的主键eid -->   
  265.             <key column="er_eid"></key>   
  266.             <!-- many-to-many属性建立和Role的多对多关联关系,class指定关联的持久化类,连接表的字段er_roleid作为外键依赖于role表的主键roleid -->   
  267.             <many-to-many class="Role" column="er_roleid"></many-to-many>   
  268.         </set>   
  269.     </class>   
  270. </hibernate-mapping>   
  271.   
  272.   
  273. Role.hbm.xml   
  274. ----------------------------------   
  275. <?xml version="1.0" encoding="UTF-8"?>   
  276. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >   
  277. <hibernate-mapping package="com.supersit.hibernate.bean">   
  278.     <class name="Role" table="role" catalog="hibernate" schema="dbo">   
  279.         <id name="roleid" column="roleid" type="int">   
  280.             <generator class="identity"></generator>   
  281.         </id>   
  282.         <property name="rolename" column="rolename" type="string"  
  283.             not-null="true">   
  284.         </property>   
  285.   
  286.         <set name="emps" table="emp_role" cascade="save-update"  
  287.             lazy="false" >   
  288.             <key column="er_roleid"></key>   
  289.             <many-to-many class="Employee" column="er_eid"></many-to-many>   
  290.         </set>   
  291.     </class>   
  292. </hibernate-mapping>   
  293.   
  294.   
  295.   
  296.   
  297. Dao   
  298. ====================================   
  299.   
  300.   
  301. EmployeeDao.java   
  302. ----------------------------------   
  303. package com.supersit.hibernate.dao;   
  304.   
  305. import java.text.ParseException;   
  306. import java.text.SimpleDateFormat;   
  307. import java.util.Date;   
  308. import java.util.HashSet;   
  309. import java.util.Iterator;   
  310. import java.util.List;   
  311. import java.util.Set;   
  312.   
  313. import org.hibernate.Hibernate;   
  314. import org.hibernate.Session;   
  315. import org.hibernate.Transaction;   
  316.   
  317. import com.supersit.hibernate.bean.Employee;   
  318. import com.supersit.hibernate.bean.Role;   
  319. import com.supersit.hibernate.exception.HibernateMsgException;   
  320.   
  321. public class EmployeeDao extends BaseDao {   
  322.   
  323.     /**  
  324.      * <p>  
  325.      * 查询所有的雇员  
  326.      * </p>  
  327.      *   
  328.      * @author chenwei  
  329.      * @return java.util.List  
  330.      * @throws HibernateMsgException  
  331.      */  
  332.     public java.util.List getEmps() throws HibernateMsgException {   
  333.         java.util.List list = new java.util.ArrayList();   
  334.         org.hibernate.Session session = null;   
  335.         org.hibernate.Transaction tran = null;   
  336.         try {   
  337.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  338.                     .getSession();   
  339.             tran = session.beginTransaction();   
  340.             list = session.createQuery("from Employee e order by e.eid").list();   
  341.   
  342.             tran.commit();   
  343.         } catch (Exception e) {   
  344.             com.supersit.hibernate.factory.HibernateSessionFactory   
  345.                     .rollbackTran(tran);   
  346.         } finally {   
  347.             com.supersit.hibernate.factory.HibernateSessionFactory   
  348.                     .closeSession(session);   
  349.         }   
  350.         return list;   
  351.     }   
  352.   
  353.     /**  
  354.      * <p>  
  355.      * 更改指定用户的角色  
  356.      * </p>  
  357.      *   
  358.      * @author chenwei  
  359.      * @param eid:用户ID  
  360.      * @param roleids:角色ID数组  
  361.      *   
  362.      */  
  363.     public static void opeRolesOfEmp(Integer eid, String[] roleids)   
  364.             throws HibernateMsgException {   
  365.         org.hibernate.Session session = null;   
  366.         org.hibernate.Transaction tran = null;   
  367.         try {   
  368.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  369.                     .getSession();   
  370.             tran = session.beginTransaction();   
  371.             Employee emp = (Employee) session.load(Employee.class, eid);   
  372.             java.util.Set roles = new java.util.HashSet();   
  373.             for (int i = 0; i < roleids.length; i++) {   
  374.   
  375.                 // 查询角色(此处应传入一个Session对象,否则会抛出org.hibernate.NonUniqueObjectException)   
  376.                 Role role = new RoleDao().getRoleById(session, new Integer(   
  377.                         roleids[i]));   
  378.                 System.out.println("rolename=" + role.getRolename());   
  379.                 roles.add(role);   
  380.             }   
  381.             emp.setRoles(roles);   
  382.             session.saveOrUpdate(emp);   
  383.             tran.commit();   
  384.         } catch (Exception e) {   
  385.             e.printStackTrace();   
  386.             com.supersit.hibernate.factory.HibernateSessionFactory   
  387.                     .rollbackTran(tran);   
  388.         } finally {   
  389.             com.supersit.hibernate.factory.HibernateSessionFactory   
  390.                     .closeSession(session);   
  391.         }   
  392.     }   
  393.   
  394.     public Employee initEmp(Employee emp) throws HibernateMsgException {   
  395.         Session session = null;   
  396.         Transaction tran = null;   
  397.         try {   
  398.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  399.                     .getSession();   
  400.             tran = session.beginTransaction();   
  401.             org.hibernate.Hibernate.initialize(emp.getRoles());   
  402.             session.update(emp);   
  403.             tran.commit();   
  404.         } catch (Exception e) {   
  405.             com.supersit.hibernate.factory.HibernateSessionFactory   
  406.                     .rollbackTran(tran);   
  407.         } finally {   
  408.             com.supersit.hibernate.factory.HibernateSessionFactory   
  409.                     .closeSession(session);   
  410.         }   
  411.         return emp;   
  412.     }   
  413.   
  414.     public static void main(String[] args) throws HibernateMsgException {   
  415.         // select();   
  416.         opeRolesOfEmp(new Integer(1), new String[] { "1""3""4" });   
  417.     }   
  418.   
  419.     // 检索   
  420.     public static void select() throws HibernateMsgException {   
  421.   
  422.         EmployeeDao dao = new EmployeeDao();   
  423.         List list = dao.getEmps();   
  424.         for (int i = 0; i < list.size(); i++) {   
  425.             Employee emp = (Employee) list.get(i);   
  426.             System.out.println("eid=" + emp.getEid());   
  427.             System.out.println("ename=" + emp.getEname());   
  428.             System.out.println("join_date=" + emp.getJoin_date()); //   
  429.             // 映射roles属性,lazy=false时,可以立即检索   
  430.             Set roles = emp.getRoles();   
  431.             java.util.Iterator ite = roles.iterator();   
  432.             while (ite.hasNext()) {   
  433.                 Role role = (Role) ite.next();   
  434.                 System.out.println("rolename=" + role.getRolename());   
  435.             }   
  436.             System.out.println("===========================================");   
  437.         }   
  438.   
  439.     }   
  440.   
  441.     // 保存对象   
  442.     public static void insert() throws HibernateMsgException {   
  443.   
  444.         java.util.Date date = new java.util.Date();   
  445.         java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  446.         try {   
  447.             date = format.parse("2007-10-17");   
  448.         } catch (ParseException e) {   
  449.             e.printStackTrace();   
  450.         }   
  451.   
  452.         Employee emp = new Employee(new Integer(1), "张信哲", date);   
  453.   
  454.         Session session = null;   
  455.         Transaction tran = null;   
  456.         try {   
  457.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  458.                     .getSession();   
  459.             tran = session.beginTransaction();   
  460.             boolean b = new EmployeeDao().saveObj(session, emp);   
  461.             if (b) {   
  462.                 System.out.println("成功保存一个Employee对象");   
  463.             }   
  464.             tran.commit();   
  465.         } catch (Exception e) {   
  466.             com.supersit.hibernate.factory.HibernateSessionFactory   
  467.                     .rollbackTran(tran);   
  468.         } finally {   
  469.             com.supersit.hibernate.factory.HibernateSessionFactory   
  470.                     .closeSession(session);   
  471.         }   
  472.   
  473.     }   
  474.   
  475. }   
  476.   
  477.   
  478.   
  479. RoleDao.java   
  480. ------------------------   
  481. package com.supersit.hibernate.dao;   
  482.   
  483. import org.hibernate.Session;   
  484. import org.hibernate.Transaction;   
  485.   
  486. import com.supersit.hibernate.bean.Employee;   
  487. import com.supersit.hibernate.bean.Role;   
  488. import com.supersit.hibernate.exception.HibernateMsgException;   
  489.   
  490. public class RoleDao extends BaseDao {   
  491.   
  492.     /**  
  493.      * <p>  
  494.      * 根据角色ID查找角色(传入一个Session对象)  
  495.      * </p>  
  496.      *   
  497.      * @param roleid:角色ID  
  498.      * @return com.supersit.hibernate.bean.Role  
  499.      * @throws HibernateMsgException  
  500.      */  
  501.     public com.supersit.hibernate.bean.Role getRoleById(Session session,   
  502.             Integer roleid) throws HibernateMsgException {   
  503.         Role role = new Role();   
  504.         try {   
  505.             java.util.List list = session.createQuery(   
  506.                     "from Role r where r.roleid=:id").setInteger("id", roleid)   
  507.                     .list();   
  508.             if (list != null && list.size() > 0)   
  509.                 role = (Role) list.get(0);   
  510.   
  511.         } catch (Exception e) {   
  512.   
  513.         }   
  514.         return role;   
  515.     }   
  516.   
  517.     /**  
  518.      * <p>  
  519.      * 根据角色ID查找角色(创建一个新的Session)  
  520.      * </p>  
  521.      *   
  522.      * @param roleid:角色ID  
  523.      * @return com.supersit.hibernate.bean.Role  
  524.      * @throws HibernateMsgException  
  525.      */  
  526.     public com.supersit.hibernate.bean.Role getRoleById(Integer roleid)   
  527.             throws HibernateMsgException {   
  528.         Role role = new Role();   
  529.         Session session = null;   
  530.         Transaction tran = null;   
  531.         try {   
  532.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  533.                     .getSession();   
  534.             tran = session.beginTransaction();   
  535.             java.util.List list = session.createQuery(   
  536.                     "from Role r where r.roleid=:id").setInteger("id", roleid)   
  537.                     .list();   
  538.             if (list != null && list.size() > 0)   
  539.                 role = (Role) list.get(0);   
  540.             tran.commit();   
  541.         } catch (Exception e) {   
  542.             com.supersit.hibernate.factory.HibernateSessionFactory   
  543.                     .rollbackTran(tran);   
  544.         } finally {   
  545.             com.supersit.hibernate.factory.HibernateSessionFactory   
  546.                     .closeSession(session);   
  547.         }   
  548.         return role;   
  549.     }   
  550.   
  551.     public static void main(String[] args) throws HibernateMsgException {   
  552.         selectEmpsOfEachRole();   
  553.     }   
  554.   
  555.     /**  
  556.      * <p>  
  557.      * 查询所有的角色  
  558.      * </p>  
  559.      *   
  560.      * @param session  
  561.      * @return java.util.List  
  562.      */  
  563.     public static java.util.List getAllRoles(org.hibernate.Session session) {   
  564.         java.util.List list = new java.util.ArrayList();   
  565.         list = session.createQuery("from Role r order by r.roleid asc").list();   
  566.         return list;   
  567.     }   
  568.   
  569.     public static void selectEmpsOfEachRole() throws HibernateMsgException {   
  570.         org.hibernate.Session session = null;   
  571.         org.hibernate.Transaction tran = null;   
  572.         try {   
  573.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  574.                     .getSession();   
  575.             tran = session.beginTransaction();   
  576.             java.util.List list = getAllRoles(session);   
  577.   
  578.             for (int i = 0; i < list.size(); i++) {   
  579.                 Role role = (Role) list.get(i);   
  580.                 System.out.println("角色编号:" + role.getRoleid());   
  581.                 System.out.println("角色名称:" + role.getRolename());   
  582.                 System.out.println("此角色下的所有雇员>>>");   
  583.                 java.util.Iterator ite = role.getEmps().iterator();   
  584.                 while (ite.hasNext()) {   
  585.                     Employee emp = (Employee) ite.next();   
  586.                     System.out.println("雇员编号:" + emp.getEid());   
  587.                     System.out.println("雇员姓名:" + emp.getEname());   
  588.                     System.out.println("入职时间:" + emp.getJoin_date());   
  589.                 }   
  590.                 System.out   
  591.                         .println("=======================================================");   
  592.             }   
  593.             tran.commit();   
  594.   
  595.         } catch (Exception e) {   
  596.             com.supersit.hibernate.factory.HibernateSessionFactory   
  597.                     .rollbackTran(tran);   
  598.         } finally {   
  599.             com.supersit.hibernate.factory.HibernateSessionFactory   
  600.                     .closeSession(session);   
  601.         }   
  602.     }   
  603.   
  604. }   
  605.   
  606.   
  607.   
  608.   
  609.   
  610.   
  611.   
  612.   
  613. /**  
  614.  *   
  615.  * <p>  
  616.  * 对于持久化类的添加,删除,修改可以通过继承这个类来实现 如果已开启Session缓存,可以传入这个Session对象进行相应的操作,当然也可以  
  617.  * 不调用此方法,直接调用Session对象相应的方法即可,在这里只是想实现面向接口编程  
  618.  * 对于没有开启Session缓存的情况,直接调用只有一个参数的方法,在方法体中会自动创建 Session对象,进行相关操作...  
  619.  * </p>  
  620.  *   
  621.  * @author chenwei  
  622.  * 2007-10-26  
  623.  *   
  624.  */  
  625. BaseDao.java   
  626. ----------------------------   
  627. package com.supersit.hibernate.dao;   
  628.   
  629. import org.hibernate.Session;   
  630.   
  631. import com.supersit.hibernate.exception.HibernateMsgException;   
  632.   
  633. public class BaseDao {   
  634.   
  635.     /**  
  636.      * <p>  
  637.      * 保存一个com.supersit.hibernate.Interface.ObjectIn对象  
  638.      * </p>  
  639.      *   
  640.      * @author chenwei  
  641.      * @param com.supersit.hibernate.Interface.ObjectIn  
  642.      * @return boolean  
  643.      * @throws HibernateMsgException  
  644.      */  
  645.     public boolean saveObj(com.supersit.hibernate.Interface.ObjectIn in)   
  646.             throws HibernateMsgException {   
  647.   
  648.         org.hibernate.Session ses = null;   
  649.         org.hibernate.Transaction tran = null;   
  650.         boolean flag = true;   
  651.         try {   
  652.             ses = com.supersit.hibernate.factory.HibernateSessionFactory1   
  653.                     .getSession();   
  654.             tran = ses.beginTransaction();   
  655.             ses.save(in);   
  656.             tran.commit();   
  657.         } catch (Exception e) {   
  658.             e.printStackTrace();   
  659.             flag = false;   
  660.             com.supersit.hibernate.factory.HibernateSessionFactory   
  661.                     .rollbackTran(tran);   
  662.         } finally {   
  663.             com.supersit.hibernate.factory.HibernateSessionFactory   
  664.                     .closeSession(ses);   
  665.         }   
  666.         return flag;   
  667.     }   
  668.   
  669.     /**  
  670.      * <p>  
  671.      * 保存一个com.supersit.hibernate.Interface.ObjectIn对象  
  672.      * </p>  
  673.      *   
  674.      * @author chenwei  
  675.      * @param com.supersit.hibernate.Interface.ObjectIn  
  676.      * @return boolean  
  677.      * @throws HibernateMsgException  
  678.      */  
  679.     public boolean saveObj(Session session,   
  680.             com.supersit.hibernate.Interface.ObjectIn in) {   
  681.         boolean flag = true;   
  682.         try {   
  683.             session.save(in);   
  684.         } catch (Exception e) {   
  685.             e.printStackTrace();   
  686.             flag = false;   
  687.         }   
  688.         return flag;   
  689.     }   
  690.   
  691.     /**  
  692.      * <p>  
  693.      * 修改一个com.supersit.hibernate.Interface.ObjectIn对象  
  694.      * </p>  
  695.      *   
  696.      * @author chenwei  
  697.      * @param com.supersit.hibernate.Interface.ObjectIn  
  698.      * @return boolean  
  699.      * @throws HibernateMsgException  
  700.      */  
  701.     public boolean updateObj(com.supersit.hibernate.Interface.ObjectIn in)   
  702.             throws HibernateMsgException {   
  703.         boolean flag = true;   
  704.         org.hibernate.Session ses = null;   
  705.         org.hibernate.Transaction tran = null;   
  706.         try {   
  707.             ses = com.supersit.hibernate.factory.HibernateSessionFactory   
  708.                     .getSession();   
  709.             tran = ses.beginTransaction();   
  710.             ses.update(in);   
  711.             tran.commit();   
  712.         } catch (Exception e) {   
  713.             flag = false;   
  714.             com.supersit.hibernate.factory.HibernateSessionFactory   
  715.                     .rollbackTran(tran);   
  716.         } finally {   
  717.             com.supersit.hibernate.factory.HibernateSessionFactory   
  718.                     .closeSession(ses);   
  719.         }   
  720.         return flag;   
  721.     }   
  722.   
  723.     /**  
  724.      * <p>  
  725.      * 修改一个com.supersit.hibernate.Interface.ObjectIn对象  
  726.      * </p>  
  727.      *   
  728.      * @author chenwei  
  729.      * @param com.supersit.hibernate.Interface.ObjectIn  
  730.      * @return boolean  
  731.      * @throws HibernateMsgException  
  732.      */  
  733.     public boolean updateObj(Session session,   
  734.             com.supersit.hibernate.Interface.ObjectIn in) {   
  735.         boolean flag = true;   
  736.         try {   
  737.             session.update(in);   
  738.         } catch (Exception e) {   
  739.             flag = false;   
  740.         }   
  741.         return flag;   
  742.     }   
  743.   
  744.     /**  
  745.      * <p>  
  746.      * 删除一个com.supersit.hibernate.Interface.ObjectIn对象  
  747.      * </p>  
  748.      *   
  749.      * @author chenwei  
  750.      * @param com.supersit.hibernate.Interface.ObjectIn  
  751.      * @return boolean  
  752.      * @throws HibernateMsgException  
  753.      */  
  754.   
  755.     public boolean deleteObj(com.supersit.hibernate.Interface.ObjectIn in)   
  756.             throws HibernateMsgException {   
  757.         boolean flag = true;   
  758.         org.hibernate.Session session = null;   
  759.         org.hibernate.Transaction tran = null;   
  760.         try {   
  761.             session = com.supersit.hibernate.factory.HibernateSessionFactory   
  762.                     .getSession();   
  763.             tran = session.beginTransaction();   
  764.             session.delete(in);   
  765.             tran.commit();   
  766.         } catch (Exception e) {   
  767.             flag = false;   
  768.             com.supersit.hibernate.factory.HibernateSessionFactory   
  769.                     .rollbackTran(tran);   
  770.         } finally {   
  771.             com.supersit.hibernate.factory.HibernateSessionFactory   
  772.                     .closeSession(session);   
  773.         }   
  774.         return flag;   
  775.     }   
  776.   
  777.     /**  
  778.      * <p>  
  779.      * 删除一个com.supersit.hibernate.Interface.ObjectIn对象  
  780.      * </p>  
  781.      *   
  782.      * @author chenwei  
  783.      * @param com.supersit.hibernate.Interface.ObjectIn  
  784.      * @return boolean  
  785.      * @throws HibernateMsgException  
  786.      */  
  787.   
  788.     public boolean deleteObj(Session session,   
  789.             com.supersit.hibernate.Interface.ObjectIn in) {   
  790.         boolean flag = true;   
  791.         try {   
  792.             session.delete(in);   
  793.         } catch (Exception e) {   
  794.             flag = false;   
  795.         }   
  796.         return flag;   
  797.     }   
  798. }   
  799.   
  800.   
  801.   
  802. ObjectIn.java   
  803. ------------------------------   
  804.   
  805. package com.supersit.hibernate.Interface;   
  806.   
  807. public interface ObjectIn {   
  808.   
  809. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值