だから私はFetchType.LAZY
コレクションでこのエンティティを持っている:なぜLazyInitializationExceptionがここで起こっていますか?
@Entity
public class Entity implements Serializable {
@OneToMany(mappedBy = "entity", fetch=FetchType.LAZY)
private List<OtherEntity> lazyCollection;
//getters and setters
}
@Entity
public class OtherEntity implements Serializable {
@ManyToOne
@JoinColumn(name = "entity", nullable = false)
private Entity entity;
}
そして、私は以下のサービスがあります。
今public class ServiceA implements Serializable {
public Entity loadEntity(Long entityId) {
return em.find(Entity.class, entityId);
}
}
public class ServiceB extends ServiceA {
public Map<? extends X, ? extends Y> load(Long entityId) {
Entity entity = loadEntity(entityId);
//play with entity and fill the map with required data
return prepareMap(entity, map);
}
//meant to be overriden in inheriting services
protected Map<? extends X, ? extends Y> prepareMap(Entity entity,
Map<? extends X, ? extends Y> map) { return map; }
}
@Stateless
public class ServiceC extends ServiceB {
@Override
protected Map<? extends X, ? extends Y> prepareMap(Entity entity,
Map<? extends X, ? extends Y> map) {
if (entity.getLazyCollection() != null
&& !entity.getLazyCollection.isEmpty()) {
// play with entity and put some other data to map
}
return map;
}
}
を、私はこのようなCDI豆からServiceB#load
を呼び出すしようとしている:
@Named
@SessionScoped
public class void WebController implements Serializable {
@EJB
private ServiceC service;
public void loadEntity(Long entityId) {
service.load(entityId);
}
}
しかし、私が得るときServiceC
entity.getLazyCollection.isEmpty()
コール、私はLazyInitializationException: illegal access to loading collection
を取得します。なぜ私はそれを取得しません。
ロード後、エンティティが何らかの形で分離されたことを意味しますか?
私も、データベースから実際のロードをトリガするentity.getLazyCollection()
を呼び出すためにServiceC
でServiceA#loadEntity
メソッドをオーバーライドしようとしたが、私はまだこのLazyInitializationException
を取得します。
JTA EntityManagerを使用していますか? –
確かに、永続化プロバイダがHibernateであるため、Hibernateによって実装されていると思います。 – jFrenetic
'EntityManager'はどうやって取得していますか? –