2013-09-03 8 views
0

프로젝트에서 두 개의 객체를 시뮬레이트하고 문제가 발생하는 간단한 테스트 프로그램을 작성하려고했습니다. 기본적으로 나는 하나의 관계가 많은 두 개의 간단한 객체, 부모와 자식을 가지고 있습니다. 나는이 프로그램을 실행하면Hibernate Eager Fetch가 작동하지 않는 이유는 무엇입니까?

import java.util.List; 
import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

import org.hibernate.Session; 
import org.hibernate.Transaction; 

import com.fhp.ems.common.data.dao.HibernateInitializer; 


@Entity 
@Table(name = "Parent") 
public class Parent { 

    private Integer id; 
    private String data; 
    private List<Child> child; 

    public Parent() {} 


    @Id 
    @Column(name = "ID", nullable = false) 
    public Integer getId() { 
    return this.id; 
    } 

    public void setId(Integer id) { 
    this.id = id; 
    } 


    @Column(name = "DATA", length = 128) 
    public String getData() { 
    return this.data; 
    } 


    public void setData(String data) { 
    this.data = data; 
    } 


    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, orphanRemoval = true) 
    @JoinColumn(name = "PID") 
    //@Fetch(value=FetchMode.SELECT) 
    public List<Child> getChild() { 
    return child; 
    } 


    public void setChild(List<Child> child) { 
    this.child = child; 
    if(child != null && child.size() > 0) { 
     for(Child c : child) { 
     this.data = c.getValue(); 
     } 
    } 
    } 



    public static void main(String[] args) { 
    Session session = null; 
    Transaction tx = null; 
    try { 
     HibernateInitializer.initLocal(); 
     session = HibernateInitializer.getSession(null); 
     int id = 101; 
     tx = session.beginTransaction(); 
     Parent p = (Parent)session.get(Parent.class, 101); 
     //Parent p = new Parent(); 
     //p.setId(id); 
     //Child c = new Child(); 
     //c.setPid(id); 
     //c.setId(1); 
     //c.setValue("child"); 
     //Child c1 = new Child(); 
     //c1.setPid(id); 
     //c1.setId(2); 
     //c1.setValue("child1"); 
     //List<Child> childs = new ArrayList<Child>(); 
     //childs.add(c); 
     //childs.add(c1); 
     //p.setChild(childs); 
     //session.saveOrUpdate(p); 

     tx.commit(); 
    } 
    catch(Exception exc) { 
     exc.printStackTrace(); 
    } 
    finally { 
     HibernateInitializer.closeSession(session, null); 
    } 

    } 

} 


import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 


//CREATE TABLE `Child` (
// `ID` int(11) NOT NULL, 
// `PID` int(11) NOT NULL,  
// `VALUE` varchar(128) default NULL, 
// PRIMARY KEY (`ID`) 
//) 


@Entity 
@Table(name = "Child") 
public class Child { 

    private Integer id; 
    private Integer pid; 
    private String value; 

    public Child() {} 

    @Id 
    @Column(name = "ID", unique = true, nullable = false) 
    public Integer getId() { 
    return this.id; 
    } 


    public void setId(Integer id) { 
    this.id = id; 
    } 


    @Column(name = "PID", nullable = false) 
    public Integer getPid() { 
    return this.pid; 
    } 


    public void setPid(Integer pid) { 
    this.pid = pid; 
    } 


    @Column(name = "VALUE", length = 128) 
    public String getValue() { 
    return this.value; 
    } 


    public void setValue(String value) { 
    this.value = value; 
    } 


} 

것은, 내가 오류 다음 얻을 : 두 개체는 다음과 나는 인출 게으른 가져 earger 변경하는 경우

Hibernate: 
    select 
     parent0_.ID as ID81_1_, 
     parent0_.DATA as DATA81_1_, 
     child1_.PID as PID81_3_, 
     child1_.ID as ID3_, 
     child1_.ID as ID82_0_, 
     child1_.PID as PID82_0_, 
     child1_.VALUE as VALUE82_0_ 
    from 
     Parent parent0_ 
    left outer join 
     Child child1_ 
      on parent0_.ID=child1_.PID 
    where 
     parent0_.ID=? 

org.hibernate.PropertyAccessException: Exception occurred inside setter 
    of com.fhp.ems.common.data.dao.test.Parent.child 
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:89) 
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:583) 
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:229) 
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3822) 
    at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152) 
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:982) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:857) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) 
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2037) 
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86) 
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76) 
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3268) 
    at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496) 
    at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477) 
    at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) 
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285) 
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152) 
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) 
    at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1005) 
    at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998) 
    at com.fhp.ems.common.data.dao.test.Parent.main(Parent.java:98) 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66) 
    ... 20 more 
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) 
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) 
    at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122) 
    at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248) 
    at com.fhp.ems.common.data.dao.test.Parent.setChild(Parent.java:71) 

을, 그것을 잘 작동합니다. @Fetch (value = FetchMode.SELECT)를 사용하면 잘 작동합니다. 내 질문은 : 열망하는 가져 오기가 작동하지 않는 이유는 무엇입니까?

+0

위한 최선의 연습이 비슷한 문제에 대한 대답은 : http://stackoverflow.com/questions/16124638/what-cause-org-hibernate -propertyaccessexception-exception-occurred-inside-sett 또한, 제 생각에는 부모와 자식 간의 단방향적인 관계가 있습니다. – paulek

답변

0

자식 테이블에 ParentId이 없을 때 어떻게 @OneToMany을 가질 수 있습니까?
단일 상위와 관련된 여러 개의 하위 키가 누락되었습니다. 테이블 ChildParentId를 추가로 그 열을 사용 @JoinColumn(name = "ParentId")

편집 :

  • 초기화 Parent.childprivate Parent pid;로 변경 private Integer pid;
  • private List<Child> child = new ArrayList<Child>();와 (과 @OneToMane mappedBy = "부모"에 추가)
  • 시험기가 아닌 주석 필드를 사용하십시오.
  • Parent.setChild() 그냥 과제물로 지정하십시오. t

그입니다 - 보통 - 부모 - 자식 관계 여기

+0

안녕하세요. 벨라 막스, 빠른 리포즈 네 주셔서 감사합니다. 예, 부모님 ID를 놓친 다음 추가했습니다. 결과는 같습니다. Lazy fetch가 작동하지 않습니다. – user1007852

+0

의견을 보내 주셔서 감사합니다. 그러나이 질문을 게시하려는 의도는 코드를 작동시키는 방법을 묻는 것이 아닙니다. 논리를 setChild 메서드에 넣지 않으면 작동 할 것임을 알았습니다. 필자의 경우에는 setChild 메소드에 로직을 넣어야하며 왜 작동하지 않는지 알고 싶다. 내 머리 꼭대기에, 열정적 인 가져 오기가 작동해야하지만 그렇지 않습니다. 나는 그것이 Hibernate 버그일지도 모른다고 들었지만, 나는 내부에 약간의 설명을하기를 알고있는 누군가를 좋아한다. – user1007852