本文介绍了使用JSF Converter时的延迟加载异常(指的是集合)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对这个问题进行了许多研究之后的第一篇文章.

This is my first post after many research on this problem.

此示例在Jboss 7.1下运行,其接缝为3.1(焊锡+持久性+面),并具有接缝管理的持久性上下文

This example is running under Jboss 7.1 with seam 3.1 (solder + persistence + faces) with seam managed persistence context

我在实体Bean上使用转换器时遇到了一个经典的failed to lazily initialize a collection, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed问题.目的是通过重用JPA模型来保持100%面向对象.

I'm facing a problem, the classical failed to lazily initialize a collection, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed when using a converter on Entity beans. The aim is to stay 100% Object oriented, by reusing the JPA model.

在beans.xml中,org.jboss.seam.transaction.TransactionInterceptor已激活

in beans.xml, org.jboss.seam.transaction.TransactionInterceptor is activated

实体bean:

@Entity
public class Member implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String email;

    @Column(name = "phone_number")
    private String phoneNumber;

    @ManyToMany
    private List<Statut> listeStatut = new ArrayList<Statut>();

    // getters, setters, hashcode, equals
}

@Entity
public class Statut implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @ManyToMany(mappedBy="listeStatut")
    private List<Member> members = new ArrayList<Member>();

    // getters, setters, hashcode, equals
}

JSF页面:

<h:form>
    <h:selectManyCheckbox id="stat" value="#{memberModif.member.listeStatut}">
        <f:converter converterId="statutConverter"/>
        <f:selectItems value="#{memberModif.statutsPossibles}" var="statut" itemValue="#{statut}" itemLabel="#{statut.name}" />
    </h:selectManyCheckbox>


    <h:commandLink id="register" action="#{memberModif.modifier()}" value="Modifier">
        <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
    </h:commandLink>
</h:form>

支持bean(我在SessionScoped之后尝试了ConversationScoped->相同的问题)

The backing bean (I tried with ConversationScoped after SessionScoped --> same problem)

@ConversationScoped
@Named
public class MemberModif implements Serializable {

    private static final long serialVersionUID = -291355942822086126L;

    @Inject
    private Logger log;

    @Inject
    private EntityManager em;

    @Inject Conversation conversation;

    private Member member;

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    public String modifier() {
        em.merge(member);
    }

    public Member getMember() {
        if (member == null) {
            member = em.createQuery("from Member m where m.id=:id",Member.class).setParameter("id", new Long(0)).getSingleResult();
        }
        return member;
    }

    public List<Statut> getStatutsPossibles() {
        return em.createQuery("from Statut", Statut.class).getResultList();
    }
}

还有转换器(强烈受接缝ObjectConverter的启发):

And the converter (strongly inspired by seam ObjectConverter) :

@FacesConverter("statutConverter")
public class StatutConverter implements Converter, Serializable {

    final private Map<String, Statut> converterMap = new HashMap<String, Statut>();
    final private Map<Statut, String> reverseConverterMap = new HashMap<Statut, String>();

    @Inject
    private transient Conversation conversation;

    private final transient Logger log = Logger.getLogger(StatutConverter.class);

    private int incrementor = 1;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (this.conversation.isTransient()) {
            log.warn("Conversion attempted without a long running conversation");
        }

        return this.converterMap.get(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (this.conversation.isTransient()) {
            log.warn("Conversion attempted without a long running conversation");
        }

        if (this.reverseConverterMap.containsKey(value)) {
            return this.reverseConverterMap.get(value);
        } else {
            final String incrementorStringValue = String.valueOf(this.incrementor++);
            this.converterMap.put(incrementorStringValue, (Statut)value);
            this.reverseConverterMap.put( (Statut)value, incrementorStringValue);
            return incrementorStringValue;
        }
    }
}

请注意,我将此转换器放在此处,以避免您通过网络搜索接缝实现,但这与使用<s:objectConverter/>标记而不是<f:converter converterId="statutConverter"/>

Please note that I put this converter here to avoid you searching over the net for the seam implementation, but it is the same as using <s:objectConverter/> tag instead of <f:converter converterId="statutConverter"/>

任何帮助都会受到感激.

Any help would be greetly appreciated.

推荐答案

看看这个:在流程验证中选择selectManyCheckbox LazyInitializationException

尝试:<f:attribute name="collectionType" value="java.util.ArrayList" />;在您的<h:selectManyCheckbox>

这篇关于使用JSF Converter时的延迟加载异常(指的是集合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:37