Note that there are some explanatory texts on larger screens.

plurals
  1. POGenericDAO with Guice, playing with Generics and ParameterizedType
    primarykey
    data
    text
    <p>most people should be familiar with making a Generic DAO for Spring + hibernate. The reference is from here <a href="http://www.ibm.com/developerworks/java/library/j-genericdao/index.html" rel="nofollow noreferrer">http://www.ibm.com/developerworks/java/library/j-genericdao/index.html</a> but there is an improvement to it on <a href="https://stackoverflow.com/questions/3888575/single-dao-generic-crud-methods-jpa-hibernate-spring">Single DAO &amp; generic CRUD methods (JPA/Hibernate + Spring)</a></p> <p>This improvement is the detection of the type as it is part of the superclass, instead of using the constructor to tell which class it is</p> <pre><code>public GenericDaoJpaImpl() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); this.entityClass = (Class&lt;T&gt;) genericSuperclass .getActualTypeArguments()[0]; } </code></pre> <p>HOWEVER, this cast will fail with Guice. To be injected, the interface and the class need to be binded in a module like this</p> <pre><code>bind(TestDao.class).to(TestDaoImpl.class); </code></pre> <p>And by doing so the constructor trick for our GenericDAO won't work because of the following:</p> <pre><code>getClass().getGenericSuperclass() = java.lang.Class getClass().getName() = com.gwtplatform.samples.basic.server.dao.TestDaoImpl$$EnhancerByGuice$$5fe0d6fd </code></pre> <p>contrary to what a Spring + Hibernate returns</p> <pre><code>getClass().getGenericSuperclass() = sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl getClass().getName() = com.gwtplatform.samples.basic.server.dao.TestDaoImpl </code></pre> <p>I'm just using a super constructor in my extended DAOs now, but still would like to obtain the type instead of providing it, any ideas?</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