Note that there are some explanatory texts on larger screens.

plurals
  1. POInjecting generic Beans with CDI/Weld
    primarykey
    data
    text
    <p>I just come from my tiny nice JavaSE/Guice world and am currently discovering the path of "carried by the container"-EE6. After having some trouble with Glassfish3.1, I just switched to JBoss and am now facing a problem that shouldnt be one.</p> <p>As infrastructural assisting class, im trying to create a generic repository/DAO for any kind of entity. In a very simple manner, this might look like this one.</p> <pre><code>public class Repository&lt;E, K extends Serializable &amp; Comparable&lt;K&gt;&gt; { private final Instance&lt;EntityManager&gt; entityManagerInstance; protected final Class&lt;E&gt; getDomainObjectClass() { return domainObjectClass; } private final Class&lt;E&gt; domainObjectClass; protected final EntityManager getEntityManager() { return entityManagerInstance.get(); } @Inject public Repository(Instance&lt;EntityManager&gt; entityManageryProvider, Provider&lt;E&gt; domainObjectProvider) { //This is a dirty hack, sadly :( domainObjectClass = (Class&lt;E&gt;)domainObjectProvider.get().getClass(); this.entityManagerInstance = entityManageryProvider; } public final void persist(E domainObject) { final EntityManager em = getEntityManager(); em.persist(domainObject); } public final Collection&lt;E&gt; getAllEntities() { final EntityManager em = getEntityManager(); final CriteriaBuilder cb = em.getCriteriaBuilder(); final CriteriaQuery&lt;E&gt; query = cb.createQuery(getDomainObjectClass()); final List&lt;E&gt; 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); } // [...] } </code></pre> <p>Now there may be a bean that does not require entity-dependent query capabilities but just a repository of a certain entity type, like (might be a test case):</p> <pre><code>public class DomainObjectARepositoryTest{ @Inject Repository&lt;DomainObjectA, PersistableUUID&gt; domainObjectARepository; @Test public void testMitarbeitererstellung() { for (DomainObjectA a : domainObjectARepository.getAllEntities()) { // do cool stuff } } } </code></pre> <p>Unfortunatly Weld does not seem to like this kind of generic injection. At deployment time, I get the following error:</p> <pre><code>state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Repository&lt;DomainObjectA , PersistableUUID&gt;] with qualifiers [@Default] at injection point [[field] @Inject sompackage.DomainObjectARepositoryTest.domainObjectARepository] </code></pre> <p>Am I missing something or did they just forgot to implement generic injects? As far as I understand the generic stuff, it is erasured after compiletime anyway - even this worked so fine in guice3 so far.</p> <p>Kind regards,</p> <p>avi</p> <p>edit: found a <a href="http://seamframework.org/Community/GenericDAO#comment79770">comment</a> by garvin king that this behavior is in the spec, but not implemented in weld, (staement was in june 2009)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload