私は Glassfish 3.1 を持っていて、oneToMany 関係を持っています。エラーが発生し続ける:
Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when a n entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, ins tantiate the LAZY relationship prior to serialization.
ユーザーエンティティ。
@Entity
@Table(name="USERS")
public class User implements Serializable
{
private static final long serialVersionUID = -8636046618352460140L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userId;
private String userName;
private int userAge;
private double latitude;
private double longitude;
public User() {
}
// getters & setters
}
グループエンティティ
@Entity
@Table(name="USERS_GROUPS")
public class Group implements Serializable
{
private static final long serialVersionUID = -9124043819358665396L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long groupId;
private String groupName;
@OneToMany(cascade=CascadeType.PERSIST, fetch=FetchType.EAGER)
@JoinTable(name="JOIN_USER_GROUP",
joinColumns={@JoinColumn(name="groupId")},
inverseJoinColumns={@JoinColumn(name="userId")})
private List<User> users = null;
public Group() {
}
// getters & setters
}
そして豆:
@PersistenceContext
private EntityManager em;
...
public Group searchGroup(String groupName) {
Query q = em.createQuery("SELECT g FROM Group g " +
"WHERE g.groupName = :name");
q.setParameter("name", groupName);
Group result = (Group) q.getResultList().get(0);
result.getUsers().size(); // << LAZY instantiation
return result;
}
// NOT TESTED
public void updateGroup(Group group)
{
em.merge(group);
}
...
クライアント テスト ドライバー:
public static void main(String[] args)
{
try
{
Context jndi = new InitialContext();
GroupManagementService service = (GroupManagementService)
jndi.lookup("java:global/GroupsManagement/GroupManagementServiceImp");
User u = new User("John", 20, 54.04, 0.4);
// the group home is already in a database
Group group = service.searchGroup("home");
group.getUsers().add(u); // << ERROR here
service.updateGroup(group);
/* IF do this */
List<User> l = group.getUsers();
if(l == null)
System.out.println("NULL");
else {
System.out.println("SIZE: "+l.size()); // << ERROR here
}
...
コードに関する提案はありますか?フォーラムに投稿された同様の質問は、残念ながら私にはうまくいきません。