2011-12-04 17 views
2

だから私は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); 
    } 
} 

しかし、私が得るときServiceCentity.getLazyCollection.isEmpty()コール、私はLazyInitializationException: illegal access to loading collectionを取得します。なぜ私はそれを取得しません。

ロード後、エンティティが何らかの形で分離されたことを意味しますか?

私も、データベースから実際のロードをトリガするentity.getLazyCollection()を呼び出すためにServiceCServiceA#loadEntityメソッドをオーバーライドしようとしたが、私はまだこのLazyInitializationExceptionを取得します。

+0

JTA EntityManagerを使用していますか? –

+0

確かに、永続化プロバイダがHibernateであるため、Hibernateによって実装されていると思います。 – jFrenetic

+0

'EntityManager'はどうやって取得していますか? –

答えて

1

根底にある例外はEntityNotFoundExceptionでした。

OtherEntityは、データベースに見つかりませんでしたSomeOtherEntityと必須の1対1の関連付けを持っています。私たちのプロジェクトのログインは適切に設定されていなかったので、ログには表示されませんでした。それ以外の場合はLazyInitializationExceptionがこの場合は奇妙なようです。 HibernateがLazyInitializationExceptionEntityNotFoundExceptionをラップするように見えます。これを行う理由は私にとっては明らかではありません。

+0

Hibernateの不思議な偏差の1つ;-) –

関連する問題