2013-09-01 2 views
0

xml로 테이블 데이터를 변환 중입니다. 나는 같은 것을 달성하기 위해 jaxb를 사용하고있다. jaxb에 대해서도 동일한 최대 절전 모드를 사용하고 있습니다. JPA 저장소를 사용하여 DB 작업을 수행하고 있습니다.JPA Hibernate JAXB 지연 초기화

내 사용자 테이블은 많은 다른 테이블에 대한 참조입니다. 하지만 사용자 및 역할 테이블의 데이터 만 필요합니다. 아래 주어진 사용자 Pojo 클래스 :

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/api] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mypersonal.model.User.UserAllowedContTypes, no session or session was closed] with root cause 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mypersonal.model.User.UserAllowedContTypes, no session or session was closed 
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:393) 
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:385) 
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:378) 
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112) 
    at org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180) 
    at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.iterator(Lister.java:267) 
    at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.iterator(Lister.java:254) 
    at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:118) 
    at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:144) 
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343) 
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:685) 
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143) 
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343) 
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:582) 
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:325) 
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483) 
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308) 
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236) 
    at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.writeToResult(Jaxb2RootElementHttpMessageConverter.java:96) 
    at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.writeInternal(AbstractXmlHttpMessageConverter.java:66) 
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179) 
    at 







@Entity 
@Table(name = "user") 
@XmlRootElement(name = "user") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class User implements Cloneable, Serializable { 

    /** Serial Version UID. */ 
    private static final long serialVersionUID = -558971439L; 

    /** Use a WeakHashMap so entries will be garbage collected once all entities 
     referring to a saved hash are garbage collected themselves. */ 
    private static final Map<Serializable, Integer> SAVED_HASHES = 
     Collections.synchronizedMap(new WeakHashMap<Serializable, Integer>()); 

    /** hashCode temporary storage. */ 
    private volatile Integer hashCode; 


    /** Field mapping. */ 
    @XmlElement(name = "activeStatus") 
    private String activeStatus; 
    /** Field mapping. */ 
    private String address; 
    /** Field mapping. */ 
    private String allowdrmcontent; 
    /** Field mapping. */ 
    private String city; 
    /** Field mapping. */ 
    private Set<CmsUserAllowedContType> cmsUserAllowedContTypes = new HashSet<CmsUserAllowedContType>(); 

    /** Field mapping. */ 
    private Set<CmsUserFtpDetails> cmsUserFtpDetailss = new HashSet<CmsUserFtpDetails>(); 



    /** Field mapping. */ 
    private String createdBy; 
    /** Field mapping. */ 
    private Date createdOn; 
    /** Field mapping. */ 
    private String email; 
    /** Field mapping. */ 
    private String firstName; 
    /** Field mapping. */ 
    private PublishingGroup fkPublisher; 
    /** Field mapping. */ 
    @XmlElement(name = "role") 
    private CmsRoleMaster fkRole; 
    /** Field mapping. */ 
    @XmlElement(name = "id") 
    private Integer id = 0; // init for hibernate bug workaround 
    /** Field mapping. */ 
    private String lastName; 
    /** Field mapping. */ 
    private Date modifiedOn; 
    /** Field mapping. */ 
    private String password; 
    /** Field mapping. */ 
    private Long phoneNumber; 
    /** Field mapping. */ 
    private Integer pincode; 
    /** Field mapping. */ 
    @XmlElement(name = "username") 
    private String username; 


    @Basic(optional = true) 
    @Column(name = "active_status" ) 
    public String getActiveStatus() { 
     return this.activeStatus; 

    } 

    public void setActiveStatus(final String activeStatus) { 
     this.activeStatus = activeStatus; 
    } 


    @Basic(optional = true) 
    @Column(length = 45 ) 
    public String getAddress() { 
     return this.address; 

    } 

    public void setAddress(final String address) { 
     this.address = address; 
    } 


    @Basic(optional = true) 
    @Column(length = 45 ) 
    public String getAllowdrmcontent() { 
     return this.allowdrmcontent; 

    } 

    public void setAllowdrmcontent(final String allowdrmcontent) { 
     this.allowdrmcontent = allowdrmcontent; 
    } 


    @Basic(optional = true) 
    @Column(length = 45 ) 
    public String getCity() { 
     return this.city; 

    } 

    public void setCity(final String city) { 
     this.city = city; 
    } 

    @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "fkUser" ) 
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE}) 
    @Basic(optional = true) 
    @Column(nullable = true ) 
    public Set<CmsUserAllowedContType> getCmsUserAllowedContTypes() { 
     return this.cmsUserAllowedContTypes; 

    } 


    public void addCmsUserAllowedContType(CmsUserAllowedContType cmsUserAllowedContType) { 
     cmsUserAllowedContType.setFkUser(this); 
     this.cmsUserAllowedContTypes.add(cmsUserAllowedContType); 
    } 


    public void setCmsUserAllowedContTypes(final Set<CmsUserAllowedContType> cmsUserAllowedContType) { 
     this.cmsUserAllowedContTypes = cmsUserAllowedContType; 
    } 

    @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "fkCmsUser" ) 
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE}) 
    @Basic(optional = true) 
    @Column(nullable = true ) 
    public Set<CmsUserFtpDetails> getCmsUserFtpDetailss() { 
     return this.cmsUserFtpDetailss; 

    } 


    public void addCmsUserFtpDetails(CmsUserFtpDetails cmsUserFtpDetails) { 
     cmsUserFtpDetails.setFkCmsUser(this); 
     this.cmsUserFtpDetailss.add(cmsUserFtpDetails); 
    } 

    public void setCmsUserFtpDetailss(final Set<CmsUserFtpDetails> cmsUserFtpDetails) { 
     this.cmsUserFtpDetailss = cmsUserFtpDetails; 
    } 



    @Basic(optional = true) 
    @Column(length = 45 ) 
    public String getCountry() { 
     return this.country; 

    } 

    public void setCountry(final String country) { 
     this.country = country; 
    } 

    @Basic(optional = false) 
    @Column(name = "created_by", nullable = false, length = 45 ) 
    public String getCreatedBy() { 
     return this.createdBy; 

    } 



    /** 
    * Set the value related to the column: createdBy. 
    * @param createdBy the createdBy value you wish to set 
    */ 
    public void setCreatedBy(final String createdBy) { 
     this.createdBy = createdBy; 
    } 

    /** 
    * Return the value associated with the column: createdOn. 
    * @return A Date object (this.createdOn) 
    */ 
    @Basic(optional = true) 
    @Column(name = "created_on" ) 
    public Date getCreatedOn() { 
     return this.createdOn; 

    } 



    /** 
    * Set the value related to the column: createdOn. 
    * @param createdOn the createdOn value you wish to set 
    */ 
    public void setCreatedOn(final Date createdOn) { 
     this.createdOn = createdOn; 
    } 

    /** 
    * Return the value associated with the column: email. 
    * @return A String object (this.email) 
    */ 
    @Basic(optional = false) 
    @Column(nullable = false, length = 45 ) 
    public String getEmail() { 
     return this.email; 

    } 



    /** 
    * Set the value related to the column: email. 
    * @param email the email value you wish to set 
    */ 
    public void setEmail(final String email) { 
     this.email = email; 
    } 

    /** 
    * Return the value associated with the column: firstName. 
    * @return A String object (this.firstName) 
    */ 
    @Basic(optional = false) 
    @Column(name = "first_name", nullable = false, length = 30 ) 
    public String getFirstName() { 
     return this.firstName; 

    } 



    /** 
    * Set the value related to the column: firstName. 
    * @param firstName the firstName value you wish to set 
    */ 
    public void setFirstName(final String firstName) { 
     this.firstName = firstName; 
    } 


    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) 
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE}) 
    @Basic(optional = false) 
    @JoinColumn(name = "fk_role_id", nullable = false) 
    public CmsRoleMaster getFkRole() { 
     return this.fkRole; 

    } 

    public void setFkRole(final CmsRoleMaster fkRole) { 
     this.fkRole = fkRole; 
    } 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Basic(optional = false) 
    @Column(name = "id", nullable = false ) 
    public Integer getId() { 
     return this.id; 

    } 

    public void setId(final Integer id) { 
     // If we've just been persisted and hashCode has been 
     // returned then make sure other entities with this 
     // ID return the already returned hash code 
     if ((this.id == null || this.id == 0) && 
       (id != null) && 
       (this.hashCode != null)) { 
     SAVED_HASHES.put(id, this.hashCode); 
     } 
     this.id = id; 
    } 

    @Basic(optional = false) 
    @Column(name = "last_name", nullable = false, length = 30 ) 
    public String getLastName() { 
     return this.lastName; 

    } 

    public void setLastName(final String lastName) { 
     this.lastName = lastName; 
    } 

    @Basic(optional = true) 
    @Column(name = "modified_on" ) 
    public Date getModifiedOn() { 
     return this.modifiedOn; 

    } 

    public void setModifiedOn(final Date modifiedOn) { 
     this.modifiedOn = modifiedOn; 
    } 

    @Basic(optional = false) 
    @Column(nullable = false, length = 45 ) 
    public String getPassword() { 
     return this.password; 

    } 

    public void setPassword(final String password) { 
     this.password = password; 
    } 

    @Basic(optional = true) 
    @Column(name = "phone_number" ) 
    public Long getPhoneNumber() { 
     return this.phoneNumber; 

    } 

    public void setPhoneNumber(final Long phoneNumber) { 
     this.phoneNumber = phoneNumber; 
    } 

    public Integer getPincode() { 
     return this.pincode; 

    } 

    public void setPincode(final Integer pincode) { 
     this.pincode = pincode; 
    } 

    @Basic(optional = false) 
    @Column(nullable = false, length = 45 ) 
    public String getUsername() { 
     return this.username; 

    } 

    public void setUsername(final String username) { 
     this.username = username; 
    } 

} 

답변

0

콜렉션을 지연로드 할 수 있도록 현재 세션 및 활성 트랜잭션이 필요합니다. 또는 콜렉션의로드 모드를 eager fetching으로 변경하십시오.

+0

동일 샘플 예제. 공용 저장소 UserRepository는 JpaRepository를 확장합니다. { \t \t 사용자 findByUsernameAndPassword (문자열 사용자 이름); \t } – user1824140

+0

하나 더 의심. 내 사용자 테이블이 10 개의 다른 테이블에 관련되어있는 경우. EAGER를 사용하면 모든 테이블의 데이터가로드됩니다. 그리고 각 테이블에 10000 개의 레코드가 있으면 내 개체가 거대합니다. 내 진술은 사실인가요? – user1824140

+0

내 질문에 대한 답변을 얻었습니다. 내 서비스 메소드에 대한 트랜잭션 주석이 트릭을 만들었습니다. 이 주석을 사용하면 최대 절전 모드 세션이 오래 지속됩니다. – user1824140