• 04hibernate_many2one_cascade


    package com.bjsxt.hibernate;

    import org.hibernate.Session;

    import junit.framework.TestCase;

    public class Many2OneTest extends TestCase {
        //忘记存一方对象group,再存多方对象user,因为多方对象user中有一个字段groupid要参考一方对象,所以会抛出异常
        public void testSave1() {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                
                Group group = new Group();
                group.setName("尚学堂");
                
                User user1 = new User();
                user1.setName("猪八戒");
                user1.setGroup(group);
                
                User user2 = new User();
                user2.setName("七仙女");
                user2.setGroup(group);
                
                //不能成功保存,抛出TransientObjectException异常
                
    //因为Group为Transient状态,oid没有分配值
                
    //persistent状态的对象是不能引用transient状态的对象的
                session.save(user1);//发Hibernate: insert into
                session.save(user2);//发Hibernate: insert into
                /**
                 * Hibernate: insert into t_user (name, groupid) values (?, ?)
                    Hibernate: insert into t_user (name, groupid) values (?, ?)
                    org.hibernate.TransientObjectException: object references an unsaved transient instance
                     - save the transient instance before flushing: com.bjsxt.hibernate.Group
                 
    */
                //也就是说:Group是一个transient,所以抛出异常
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }
        }    
        //先存一方对象group,再存多方对象user,OK!
        public void testSave2() {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                
                Group group = new Group();
                group.setName("尚学堂");
                
                session.save(group);
                
                User user1 = new User();
                user1.setName("猪八戒2");
                user1.setGroup(group);
                
                User user2 = new User();
                user2.setName("七仙女2");
                user2.setGroup(group);
                
                //可以正确存储
                session.save(user1);
                session.save(user2);
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }
        }    
        //先设置级联cascade属性,然后在保存的时候,只需要保存多方对象user,采用cascade属性,ok
        public void testSave3() {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                
                Group group = new Group();
                group.setName("尚学堂");
                
                User user1 = new User();
                user1.setName("菜102");
                user1.setGroup(group);
                
                User user2 = new User();
                user2.setName("容祖儿2");
                user2.setGroup(group);
                
                //不会抛出异常,因为采用了cascade属性,所以它会先保存Group
                
    //采用cascade属性是解决TransientObjectException异常的一种手段            
                session.save(user1);
                session.save(user2);
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }
        }    
        
        public void testLoad1() {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                User user = (User)session.load(User.class, 8);
                System.out.println("user.name=" + user.getName());
                System.out.println("user.group.name=" + user.getGroup().getName());
                
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }
        }    
        
    }
  • 相关阅读:
    法正(25):劝降
    单例模式
    Redis学习笔记(六)---List
    canvas的使用
    HTML5的新特性
    html学习笔记一
    matlab无法使用
    Hadoop笔记(一)
    PL/SQL笔记(一)
    Oracle数据库(二)
  • 原文地址:https://www.cnblogs.com/alamps/p/2619042.html
Copyright © 2020-2023  润新知