3

这是我对这个问题进行多次研究后的第一篇文章。

这个例子在 Jboss 7.1 下运行,接缝 3.1(焊料 + 持久性 + 面)和接缝管理持久性上下文

我遇到了一个问题,这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是在实体 bean 上使用转换器时的经典问题。目标是通过重用 JPA 模型来保持 100% 面向对象。

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

实体豆:

@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 --> 同样的问题)

@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();
    }
}

和转换器(受到 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"/>

任何帮助将不胜感激。

4

2 回答 2

1

您应该访问同一事务中的对象。如果您确定您已经这样做了,您可以尝试通过在上下文中查找实体管理器而不是注入它来获取它。我有一个类似的问题,就是这样解决的。当您第一次获得对它的引用时,您还可以在事务中初始化集合。

Hibernate.initialize(yourCollection);  
于 2012-07-08T04:38:30.030 回答
1

看看这个:selectManyCheckbox LazyInitializationException on process validation

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

于 2014-02-05T21:02:42.040 回答