简体   繁体   English

JPA使用struts2映射单向@ManyToMany

[英]JPA mapping unidirectional @ManyToMany with struts2

Hy guys, 大家好

I have a problem with the @OneToMany unidirectional association. 我对@OneToMany单向关联有@OneToMany Basically in my model I have two entities: Player and Role . 基本上在我的模型中,我有两个实体: PlayerRole I map this relationship with a @ManyToMany cause: One player can have more Role and a Role can be associated with multiple Players. 我将这种关系与@ManyToMany映射为原因:一个玩家可以拥有更多角色,并且一个角色可以与多个玩家关联。

Player.java 播放器

@Entity
public class Player implements Serializable {
    ...
    @ManyToMany
    private List<Role> roles;
    ...
}

Role.java 角色.java

@Entity
public class Role implements Serializable {
    ...
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String roleName;
    ...
}

I print my player list with struts2 like this: 我用struts2这样打印我的播放器列表:

<table>
...
   <s:iterator value="players" status="player">
     <tr>
     <td><s:property value="name"/></td>
     <td><s:property value="price"/></td>
     <td>
         <s:iterator value="roles" status="role">
             <s:property value="roleName"/>
         </s:itetator>
     </td>
  </s:iterator>
...
</table>

And I get this error: 我得到这个错误:

org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: no session or session was closed.

Why I'm getting this error? 为什么我会收到此错误? It is wrong the JPA mapping? JPA映射错了吗? I tried to remove (just for a check) the role inner iterator and I can see the players table well, but obviously without roles that I need. 我尝试删除(仅作检查)角色内部迭代器,可以很好地看到玩家表,但显然没有我需要的角色。

NOTE: I tried to debug the application and when I was in the action, I got the List<Player> , I expanded one Player and I saw the List<Roles> with a Persistent Bag variable, I tried to maximized again but I was unable to get the roleName variable. 注意:我尝试调试该应用程序,当我执行操作时,得到了List<Player> ,我展开了一个Player,然后看到带有Persistent Bag变量的List<Roles> ,我尝试再次最大化,但我无法获取roleName变量。

Can someone help me to figure out the problem? 有人可以帮我解决问题吗? Thanks in advance. 提前致谢。

You are trying to access hibernate result which is lazy initialized after closing your hibernate session. 您正在尝试访问休眠结果,该结果在关闭休眠会话后被延迟初始化。 That's why you are getting this error. 这就是为什么您会收到此错误。 Change initialization type to eager by fetchType. 通过fetchType将初始化类型更改为eager。

Actually, if the EAGER solution does not work, I think your only option is to keep the transaction open while you fetch all the data. 实际上,如果EAGER解决方案不起作用,我认为您唯一的选择是在获取所有数据时保持事务打开。 So basically: 所以基本上:

  1. open transaction. 公开交易。

2.- Query the database. 2.-查询数据库。

3.- fetch all data and move it to another data structure (some DTO) that will containt only the data that you want. 3.-提取所有数据并将其移至仅包含所需数据的另一个数据结构(某些DTO)。 This will go to the roles as well. 这也将成为角色。

4.- close transaction. 4.-平仓交易。

5.- generate response using the DTO structure 5.-使用DTO结构生成响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM