使用JPA 2和EJB 3.1“没有会话或会话已关闭” [英] 'no session or session was closed' with JPA 2 and EJB 3.1
问题描述
我有这种方法的无状态会话bean:
@Override
public List< Character> getUserCharacters(int userId){
User user = em.find(User.class,userId);
if(user!= null)
返回user.getCharacters();
else
返回null;
其中用户
类如果以这种方式定义的话:
@Entity
@Table(name =Users)
public class User实现Serializable {
/ ** * /
private static final long serialVersionUID = 8119486011976039947L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
@ManyToMany(fetch = FetchType.EAGER)
private Set< Role>角色;
@OneToMany(mappedBy =owner,fetch = FetchType.LAZY)
private List< com.AandP.game.model.characters.Character>字符;
$ b $ public User(){
creationDate = new Date();
roles = new HashSet< Role>();
$ / code>
但是当我执行这个方法时(从我的<$
org.hibernate.LazyInitializationException:失败c $ c> @Named
bean)我收到异常:懒惰地初始化一个角色集合:com.AandP.game.model.User.characters,没有会话或会话被关闭
根据JPA 2.0规范,会话应该在整个事务中保持活跃状态。在这种情况下,交易(在我看来)持续一个完整的方法调用(对于类或方法没有额外的事务属性)。
所以问题是:什么是这段代码有问题,我怎么才能加载类属性。
$ b $ JPA 2.0规范会话应该在整个事务中保持活跃状态。在这种情况下,交易(在我看来)持续一个完整的方法调用(在类或方法上没有额外的事务属性)。这是真的,但它不包括所返回对象的序列化。
我将导出会话bean的问题与web服务相同。详细了解此处的问题。
如果您有类似的用法,强烈建议您返回普通对象而不是实体。你可以像我们一样使用一些bean映射框架。我们使用了推土机。
I have stateless session bean with this method:
@Override
public List<Character> getUserCharacters(int userId) {
User user = em.find(User.class, userId);
if(user != null)
return user.getCharacters();
else
return null;
}
where User
class if defined in this way:
@Entity
@Table(name="Users")
public class User implements Serializable{
/** */
private static final long serialVersionUID = 8119486011976039947L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;
@ManyToMany(fetch=FetchType.EAGER)
private Set<Role> roles;
@OneToMany(mappedBy="owner",fetch=FetchType.LAZY)
private List<com.AandP.game.model.characters.Character> characters;
public User() {
creationDate = new Date();
roles = new HashSet<Role>();
}
}
But when i execute this method (from my @Named
bean) i receive exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.AandP.game.model.User.characters, no session or session was closed
According to the JPA 2.0 specification session should stay alive for a whole transaction. In this situation transaction (in my opinion) last for a whole method call (there is no additional transaction attributes on class or method).
So the question is: what is wrong with this code and how can I load class properties in a lazy way.
According to the JPA 2.0 specification session should stay alive for a whole transaction. In this situation transaction (in my opinion) last for a whole method call (there is no additional transaction attributes on class or method).
That's true, but it does not include the serialization of the returned objects.
I had the same issue exporting a session bean as a web service. Read more about the issue here.
If you have a similar use would strongly suggest you to return plain objects and not entities. You can use some bean mapping framework like we did. We used Dozer.
这篇关于使用JPA 2和EJB 3.1“没有会话或会话已关闭”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!