私は小さな素敵な JavaSE/Guice の世界から来たばかりで、現在「コンテナによって運ばれる」EE6 の道を発見しています。Glassfish3.1 で問題が発生した後、JBoss に切り替えたところ、問題が発生するはずがありません。
インフラ支援クラスとして、あらゆる種類のエンティティ用の汎用リポジトリ/DAO を作成しようとしています。非常に単純な方法で、これは次のようになります。
public class Repository<E, K extends Serializable & Comparable<K>> {
private final Instance<EntityManager> entityManagerInstance;
protected final Class<E> getDomainObjectClass() {
return domainObjectClass;
}
private final Class<E> domainObjectClass;
protected final EntityManager getEntityManager() {
return entityManagerInstance.get();
}
@Inject
public Repository(Instance<EntityManager> entityManageryProvider, Provider<E> domainObjectProvider) {
//This is a dirty hack, sadly :(
domainObjectClass = (Class<E>)domainObjectProvider.get().getClass();
this.entityManagerInstance = entityManageryProvider;
}
public final void persist(E domainObject) {
final EntityManager em = getEntityManager();
em.persist(domainObject);
}
public final Collection<E> getAllEntities() {
final EntityManager em = getEntityManager();
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<E> query = cb.createQuery(getDomainObjectClass());
final List<E> result = em.createQuery(query).getResultList();
return Collections.unmodifiableList(result);
}
public final E find(K id) {
Preconditions.checkNotNull(id);
final EntityManager em = getEntityManager();
return em.find(getDomainObjectClass(), id);
}
// [...]
}
現在、エンティティ依存のクエリ機能を必要とせず、次のような特定のエンティティ タイプのリポジトリのみを必要とする Bean が存在する場合があります (テスト ケースである可能性があります)。
public class DomainObjectARepositoryTest{
@Inject
Repository<DomainObjectA, PersistableUUID> domainObjectARepository;
@Test
public void testMitarbeitererstellung() {
for (DomainObjectA a : domainObjectARepository.getAllEntities()) {
// do cool stuff
}
}
}
残念ながら、Weld はこの種のジェネリック インジェクションを好まないようです。展開時に、次のエラーが発生します。
state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Repository<DomainObjectA , PersistableUUID>] with qualifiers [@Default] at injection point [[field] @Inject sompackage.DomainObjectARepositoryTest.domainObjectARepository]
私は何かを見逃していますか、それともジェネリック注入を実装するのを忘れていましたか? 私が一般的なものを理解している限り、それはとにかくコンパイル時に消去されます - これは今のところ guice3 でとてもうまくいきました。
敬具、
アビ
編集:この動作は仕様にあるが、溶接には実装されていないというガービン・キングのコメントを見つけました(声明は2009年6月にありました)