JPA Hibernate JAXB Lazy初始化

时间:2013-09-01 16:03:25

标签: hibernate jpa jaxb

我正在将表数据转换为xml。我正在使用jaxb来实现同样的目标。我也在为jaxb使用相同的hibernate pojo。我正在使用JPA存储库来执行数据库操作。

我的用户表是许多其他表中的引用。但我只想要来自用户和角色表的数据。用户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;
    }

}

1 个答案:

答案 0 :(得分:0)

您需要当前会​​话和活动事务,以便可以延迟加载集合。或者,将集合的加载模式更改为急切提取。