0

I got the below Exception when i wnat to get the data from another table

17:38:45,823 ERROR [org.hibernate.LazyInitializationException] failed to lazily initialize a collection of role: com.sinergia.ea.model.TypeOfArtifactModel.checkedItems, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.sinergia.ea.model.TypeOfArtifactModel.checkedItems, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) [:3.2.6.ga]
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) [:3.2.6.ga]
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343) [:3.2.6.ga]
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) [:3.2.6.ga]
        at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309) [:3.2.6.ga]
        at java.lang.String.valueOf(String.java:2826) [:1.6.0_18]
        at java.lang.StringBuilder.append(StringBuilder.java:115) [:1.6.0_18]
        at com.sinergia.ea.view.TypeOfArtifactBean.editTypeOfArtifact(TypeOfArtifactBean.java:203) [:]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_18]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_18]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_18]
        at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_18]
        at org.apache.el.parser.AstValue.invoke(AstValue.java:196) [:6.0.0.Final]
        at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) [:6.0.0.Final]
        at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [:2.1.3-SNAPSHOT]
        at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [:2.1.0-FCS]
        at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [:2.1.3-SNAPSHOT]
        at javax.faces.component.UICommand.broadcast(UICommand.java:315) [:2.1.0-FCS]
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [:2.1.0-FCS]
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [:2.1.0-FCS]
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [:2.1.3-SNAPSHOT]
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [:2.1.3-SNAPSHOT]
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [:2.1.3-SNAPSHOT]
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409) [:2.1.0-FCS]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]

Model Class :

@Entity
@Table(name="TYPE_OF_ARTIFACT")
public class TypeOfArtifactModel implements java.io.Serializable , Identifiable{

    /**
     * 
     */
    private static final long serialVersionUID = 2662289176706818360L;



    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TYPE_OF_ARTIFACT_SEQ")
    @SequenceGenerator(name = "TYPE_OF_ARTIFACT_SEQ", sequenceName = "TYPE_OF_ARTIFACT_SEQ")
    @Column(name="ID",unique=true, nullable=false)
    private Integer id;

    @Column(name="DESCRIPTION", nullable=true, length=400)
    private String description;

    @Column(name="NAME", nullable=false, length=50)
    private String name;

    @OneToMany(targetEntity = AdditionalInfoModel.class, mappedBy = "typeOfArtifactID",cascade = CascadeType.ALL)
    private Set<AdditionalInfoModel> additionalInfos = new HashSet<AdditionalInfoModel>(0);


    @OneToMany
    @JoinTable(name = "TYPE_ARTIFACT_OPERATE_RELATION", joinColumns = { @JoinColumn(name = "TYPE_OF_ARTIFACT_ID") }, inverseJoinColumns = { @JoinColumn(name = "OPERATE_ARTIFACT_ID") })
    private Set<TypeOfArtifactModel> checkedItems = new HashSet<TypeOfArtifactModel>(0);



    public TypeOfArtifactModel() {
    }

View bean

public String editTypeOfArtifact() {
        additionalInfoModelList = additionalInfoService.getAdditionalInfoList(getCurrentItem());
        setAdditionalInfoModelList(additionalInfoModelList);
        if(getCurrentItem() != null){
            Set<TypeOfArtifactModel> typeOfArtifactCheckedItems = getCurrentItem().getCheckedItems();
            System.out.println("TypeOfArtifactCheckedItems :"+typeOfArtifactCheckedItems);
            if(typeOfArtifactModelList != null && !(typeOfArtifactModelList.isEmpty())){
                if(typeOfArtifactCheckedItems != null && !(typeOfArtifactCheckedItems.isEmpty())){
                for (TypeOfArtifactModel item : typeOfArtifactModelList) {
                    for (TypeOfArtifactModel checkedItem : typeOfArtifactCheckedItems) {
                        if(item.getId()==checkedItem.getId()){
                            checked.put(checkedItem.getId().longValue(), true);
                        }
                    }
                }
            }
         }
        }

        return null;
    }

Set typeOfArtifactCheckedItems = getCurrentItem().getCheckedItems();

i got the above error at particular situation why it happens i don't know please give me a solution.

1 Answer 1

2

By default a @OneToMany collection e.g. checkedItems is lazily loaded by Hibernate. This means that when you load a TypeOfArtifactModel a proxy is created in place of the actual collection.

When you first access that collection Hibernate will attempt to load the required entities from the database. However, that load must take place using the same session as the original entity. In your case it appears that the session has been closed before you try to access the collection.

To avoid this you need to either:

  • Make the collection eager loaded using FetchType.EAGER (this is not advised)
  • Fetch the collection when you fetch the TypeOfArtifactModel using a HQL fetch join or using Hibernate.initialize(model.getCheckedItems())

You haven't shown any of your transaction management code but make sure you do the above before the session is closed (or transaction committed).

It's also worth pointing out that this gets asked about once a day on Stack Overflow.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.