3

selectManyCheckboxの値を保存および編集するための文字列のセットを持つフィールドがあります。http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.htmlで説明されているように、注釈 @ElementCollection を使用します 。最初は保存できますが、2 回目に保存すると、次の例外が発生します。

原因: javax.servlet.ServletException: コレクションの遅延初期化に失敗しました。javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) [jboss-jsf-api_2.1_spec-2.0. 0.Final.jar:2.0.0.Final] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.10.Final.jar:] at org.apache.catalina. core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:] org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79) [primefaces-3.3.jar:] ] org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.10.Final.jar:] org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:] org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012- 02-10 15:31] org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) で [jbossweb-7.0.10.Final.jar:] org.apache.catalina.core.ApplicationFilterChain.doFilter で(ApplicationFilterChain.java:248) [jbossweb-7.0.10.Final.jar:] org.jboss.solder.servlet.exception.CatchExceptionFilter.doFilter(CatchExceptionFilter.java:65) で [solder-impl-3.1.0.Final .jar:3.1.0.Final] ... 29 以上 原因: org.hibernate.LazyInitializationException: コレクションの遅延初期化に失敗しました。org.hibernate.collection.internal.AbstractPersistentCollection でセッションまたはセッションが閉じられませんでした。throwLazyInitializationException(AbstractPersistentCollection.java:393) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:385) [hibernate-core -4.0.1.Final.jar:4.0.1.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:378) [hibernate-core-4.0.1.Final.jar:4.0.1 .Final] at org.hibernate.collection.internal.PersistentSet.add(PersistentSet.java:206) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] at com.sun.faces.renderkit. html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:382) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:129) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:315) で[jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030) で [jboss-jsf-api_2.1_spec-2.0.0 .Final.jar:2.0.0.Final] at javax.faces.component.UIInput.validate(UIInput.java:960) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final ] で javax.faces.component.UIInput.executeValidate(UIInput.java:1233) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final] で javax.faces.component.UIInput. processValidators(UIInput.java:698) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final] javax.faces.component.UIForm.processValidators(UIForm.java:253) [jboss -jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final] at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final] at javax.faces.component.UIViewRoot .processValidators(UIViewRoot.java:1172) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final] com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) ) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.5- jbossorg-1.jar:2.1.5-SNAPSHOT] at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5- SNAPSHOT] at org.apache.myfaces.extensions.cdi.jsf2.impl.listener.phase.CodiLifecycleWrapper.execute(CodiLifecycleWrapper.java:95) [myfaces-extcdi-jsf20-module-impl-1.0.5.jar:1.0. 5] javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Final.jar:2.0.0.Final] ... 38 もっと見る

JBOSS 7.1.0-Final、Hibernate 4.0.1.Final、および JSF 2.0 を使用しています

以下は私のコードです:

テスト.java

@Entity
@Table(name = "TEST")
@NamedQueries({
    @NamedQuery(name = Test.FIND_BY_ID, query = "select test from Test test where test.id = :id")
})
public class Test implements Serializable {

    private static final long serialVersionUID = -7294677843656741933L;

    public static final String FIND_BY_ID = "test.by.id";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ElementCollection(fetch=FetchType.EAGER)
    private Set<String> textes;


    public Test() {
    }

    public Long getId() {
        return id;
    }

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

    public Set<String> getTextes() {
        return textes;
    }

    public void setTextes(Set<String> textes) {
        this.textes = textes;
    }
}

test.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<body>
    <h:form id="form">
        <h:selectManyCheckbox value="#{formBean.test.textes}">
            <f:selectItem itemLabel="Option 1" itemValue="Option 1" />  
            <f:selectItem itemLabel="Option 2" itemValue="Option 2" />  
            <f:selectItem itemLabel="Option 3" itemValue="Option 3" />  
        </h:selectManyCheckbox>

        <h:commandButton value="Save" action="#{formBean.saveTest}" />
    </h:form>
</body>
</html>

関連するソースは次のとおりです。

FormBean.java

@Named
@SessionScoped
public class FormBean implements Serializable {

    private static final long serialVersionUID = 6484233109393875203L;

    @EJB
    private ServiceBean serviceBean;

    private Test test;

    public String saveTest() {
        serviceBean.save(test);
        return "test";
    }

    public String createTest() {
        test = new Test();
        return "test";
    }

    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }

}

ServiceBean.java

@Stateless
@LocalBean
public class ServiceBean {

    @PersistenceContext
    private EntityManager entityManager;

    public boolean save(Test test) {
        Test dbTest = findTestById(test.getId());
        if (dbTest == null) {
            entityManager.persist(test);
        }
        else {
            entityManager.merge(test);
        }

        return true;
    }

    public Test findTestById(Long id) {
        if (id == null)
            return null;

        TypedQuery<Test> query = entityManager.createNamedQuery(Test.FIND_BY_ID, Test.class);
        query.setParameter("id", id);
        List<Test> results = query.getResultList();

        if (results.isEmpty())
            return null;

        return results.get(0);
    }
}

test_index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<body>
    <h:form>
        <h:commandLink value="Test" action="#{formBean.createTest()}" />
    </h:form>
</body>
</html>

グエンを助けてくれてありがとう

4

2 に答える 2

1

この問題の解決策を見つけました。これは、@ElementCollection で既に公開されています

以下は私の解決策です:

テスト.java

@Entity
@Table(name = "TEST")
@NamedQueries({
    @NamedQuery(name = Test.FIND_BY_ID, query = "select test from Test test where test.id = :id")
})
public class Test implements Serializable {

    private static final long serialVersionUID = -7294677843656741933L;

    public static final String FIND_BY_ID = "test.by.id";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ElementCollection
    private Set<String> textes;

    public Test() {
    }

    public Long getId() {
        return id;
    }

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

    public Set<String> getTextes() {
        return textes;
    }

    public void setTextes(Set<String> textes) {
        this.textes = textes;
    }
}

test.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<body>
    <h:form id="form">
        <h:selectManyCheckbox value="#{formBean.selectedTextes}">
            <f:selectItem itemLabel="Option 1" itemValue="Option 1" />  
            <f:selectItem itemLabel="Option 2" itemValue="Option 2" />  
            <f:selectItem itemLabel="Option 3" itemValue="Option 3" />  
        </h:selectManyCheckbox>

        <h:commandButton value="Save" action="#{formBean.saveTest}" />
    </h:form>
</body>
</html>

FormBean.java

@Named
@SessionScoped
public class FormBean implements Serializable {

    private static final long serialVersionUID = 6484233109393875203L;

    @EJB
    private ServiceBean serviceBean;

    private Test test;

    private Set<String> selectedTextes;

    public String saveTest() {
        serviceBean.save(test, selectedTextes);
        return "test";
    }

    public String createTest() {
        test = new Test();
        return "test";
    }

    // getters and setters
}

ServiceBean.java

@Stateless
@LocalBean
public class ServiceBean {

    @PersistenceContext
    private EntityManager entityManager;

    public boolean save(Test test, Set<String> selectedTextes) {
        Test dbTest = findTestById(test.getId());
        if (dbTest == null) {
            if (selectedTextes != null) {
                test.setTextes(selectedTextes);
            }
            entityManager.persist(test);
        }
        else {
            if (selectedTextes != null) {
                dbTest.setTextes(selectedTextes);
            }
            entityManager.merge(dbTest);
        }

        return true;
    }

    public Test findTestById(Long id) {
        if (id == null)
            return null;

        TypedQuery<Test> query = entityManager.createNamedQuery(Test.FIND_BY_ID, Test.class);
        query.setParameter("id", id);
        List<Test> results = query.getResultList();

        if (results.isEmpty())
            return null;

        return results.get(0);
    }
}

test_index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<body>
    <h:form>
        <h:commandLink value="Test" action="#{formBean.createTest()}" />
    </h:form>
</body>
</html>

グエン

于 2012-07-19T10:10:04.550 に答える
0

質問と回答の違い:

***************
*** 14,18 ****
        private Long id;

!       @ElementCollection(fetch=FetchType.EAGER)
        private Set<String> textes;

--- 14,18 ----
        private Long id;

!       @ElementCollection
        private Set<String> textes;

***************
*** 47,51 ****
      <body>
        <h:form id="form">
!           <h:selectManyCheckbox value="#{formBean.test.textes}">
                <f:selectItem itemLabel="Option 1" itemValue="Option 1" />
                  <f:selectItem itemLabel="Option 2" itemValue="Option 2" />
--- 47,51 ----
      <body>
        <h:form id="form">
!           <h:selectManyCheckbox value="#{formBean.selectedTextes}">
                <f:selectItem itemLabel="Option 1" itemValue="Option 1" />
                  <f:selectItem itemLabel="Option 2" itemValue="Option 2" />
***************
*** 70,75 ****
        private Test test;

        public String saveTest() {
!           serviceBean.save(test);
            return "test";
        }
--- 70,77 ----
        private Test test;

+       private Set<String> selectedTextes;
+ 
        public String saveTest() {
!           serviceBean.save(test, selectedTextes);
            return "test";
        }
***************
*** 97,107 ****
        private EntityManager entityManager;

!       public boolean save(Test test) {
            Test dbTest = findTestById(test.getId());
            if (dbTest == null) {
                entityManager.persist(test);
            }
            else {
!               entityManager.merge(test);
            }

--- 99,115 ----
        private EntityManager entityManager;

!       public boolean save(Test test, Set<String> selectedTextes) {
            Test dbTest = findTestById(test.getId());
            if (dbTest == null) {
+               if (selectedTextes != null) {
+                   test.setTextes(selectedTextes);
+               }
                entityManager.persist(test);
            }
            else {
!               if (selectedTextes != null) {
!                   dbTest.setTextes(selectedTextes);
!               }
!               entityManager.merge(dbTest);
            }
于 2014-06-25T12:15:06.617 に答える