한 쌍 다/다 중 캐 스 케 이 드 속성 inverse 속성 에 따 른 기록

8189 단어 HibernateBlog
기본 설정 동작:
http://yafei.iteye.com/blog/799999
inverse 속성:
http://www.iteye.com/topic/2633
inverse 속성 및 hibenate 기타 지식(이해 하기 쉬 운):
http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=3479
	<class name="org.onetomany.bo.Child" table="CHILD">    
                <id name="id" column="ID" type="integer">    
                    <generator class="sequence">
						<param name="sequence">SEQ_ID</param>
					</generator>    
                </id>    
        <many-to-one name="parent" class="org.onetomany.bo.Parent" cascade="save-update"  column="PARENTID"/>    
    </class>    
    <class name="org.onetomany.bo.Parent" table="PARENT">    
                <id name="parentId" column="PARENTID" type="integer">    
                    <generator class="sequence">
						<param name="sequence">SEQ_PAERENTID</param>
					</generator>      
                </id>    
        <bag name="child"  inverse="true" cascade="all">    
             <key column="PARENTID"/>    
             <one-to-many class="org.onetomany.bo.Child"/>    
        </bag>    
    </class> 
		Session s=null;
		Transaction tx=null;
		try{
			s=HibernateSessionFactory.getSession();
			tx=s.beginTransaction();    //hibernate         ,        。
			
			Parent parent = new Parent();
            Child child = new Child();  
            Child child2 = new Child();  
            Child child3 = new Child();  
            List list = new ArrayList();    
            list.add(child);   
            list.add(child2);
            list.add(child3);
            parent.setChild(list);   
            System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddd");  
            
            child.setParent(parent);
            child2.setParent(parent);
            child3.setParent(parent);   
            s.save(child);;    
            s.flush();;    
            System.out.println("llllllllllllllllllllllllllll");   
			tx.commit();
		}catch(HibernateException e){
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}finally{
			if(s!=null){
				s.close();
			}
		}

캐 스 케 이 드 속성: 
parent 의 캐 스 케 이 드 를 제거 합 니 다="all"
Hibernate: select SEQ_PAERENTID.nextval from dual
Hibernate: insert into PARENT (PARENTID) values (?)
dddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: select SEQ_ID.nextval from dual
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
ID          PARENTID   
----------- -----------   
         74           19  
parent 의[color=red]캐 스 케 이 드="all"을 제거 하지 않 습 니 다.
dddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: select SEQ_ID.nextval from dual
Hibernate: select SEQ_PAERENTID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: insert into PARENT (PARENTID) values (?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
llllllllllllllllllllllllllll
ID          PARENTID   
----------- -----------   
         74           19   
         75           19  
         76           19
parent 의[color=red]inverse="true"속성 을 제거 합 니 다.
dddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: select SEQ_ID.nextval from dual
Hibernate: select SEQ_PAERENTID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: insert into PARENT (PARENTID) values (?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: update CHILD set PARENTID=? where ID=?
Hibernate: update CHILD set PARENTID=? where ID=?
Hibernate: update CHILD set PARENTID=? where ID=?
llllllllllllllllllllllllllll
ID          PARENTID   
----------- -----------   
         74           19   
         75           19  
         76           19
update 를 세 번 더 했 습 니 다.성능 문제 가 있 습 니 다.
다음 코드 로 변경:
Session s=null;
		Transaction tx=null;
		try{
			s=HibernateSessionFactory.getSession();
			tx=s.beginTransaction();    //hibernate         ,        。
			
			Parent parent = new Parent();;    
            Child child = new Child();;    
            Child child2 = new Child();;    
            List list = new ArrayList();;    
            list.add(child);;    
            list.add(child2);;    
            parent.setChild(list);;    
            s.save(parent);;    
            s.flush();;    
            System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddd"); ;    
            Child child3 = new Child();;  
            child3.setParent(parent);;  
//            child.setParent(parent);
//            child2.setParent(parent);
            s.save(child3);;  
//            s.save(child);;
//            s.save(child2);;
            System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); ;    
			tx.commit();
		}catch(HibernateException e){
			if(tx!=null){
				tx.rollback();
			}
			throw e;
		}finally{
			if(s!=null){
				s.close();
			}
		}	

parent 의[color=red]inverse="true"속성 을 제거 합 니 다.
Hibernate: select SEQ_PAERENTID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: insert into PARENT (PARENTID) values (?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: update CHILD set PARENTID=? where ID=?
Hibernate: update CHILD set PARENTID=? where ID=?
dddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: select SEQ_ID.nextval from dual
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
ID          PARENTID   
----------- -----------   
         74           19   
         75           19  
         76           19
parent 의[color=red]inverse="true"속성 을 제거 하지 않 습 니 다.
Hibernate: select SEQ_PAERENTID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: select SEQ_ID.nextval from dual
Hibernate: insert into PARENT (PARENTID) values (?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
dddddddddddddddddddddddddddddddddddddddddddddddddddddd
Hibernate: select SEQ_ID.nextval from dual
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Hibernate: insert into CHILD (PARENTID, ID) values (?, ?)
ID          PARENTID   
----------- -----------   
         74           -   
         75           -  
         76           19

좋은 웹페이지 즐겨찾기