Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to inject a service within abstract class using generics
    primarykey
    data
    text
    <p>I am attempting to create a generic abstract service class that provides common crud operations to my service layer by bringing together Service layer Objects (DTO?), Data layer Entities and their corresponding DAOs.</p> <p>The DAO layer is standard-issue abstraction where my DAOs extend an AbstractJpaImpl:</p> <pre><code>@Repository public abstract class AbstractJpaBaseDaoImpl&lt;K extends Serializable, E&gt; implements BaseDao&lt;K, E&gt; { //Dao implementation } </code></pre> <p>The DAOs extend this class and implement their respective dao interface that extends BaseDao.</p> <p>I wish to create something similar in my service layer, but how would i inject the dao itself?</p> <pre><code>public abstract class AbstractBaseCrudServiceImpl&lt;K extends Serializable, B extends AbstractBaseCrudBean, P, D extends AbstractJpaBaseDaoImpl&lt;K,P&gt;&gt; implements BaseCrudService&lt;K, B&gt; { protected Class&lt;B&gt; businessObject; protected Class&lt;P&gt; persistObject; protected Class&lt;D&gt; dao; @SuppressWarnings("unchecked") public AbstractBaseCrudServiceImpl() { //Extract the class type by accessing this classes parameters by index &lt;0,1...&gt; so 0 is K and 1 is E. this.businessObject = (Class&lt;B&gt;) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; this.persistObject = (Class&lt;P&gt;) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[2]; this.dao = (Class&lt;D&gt;) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[3]; } //stuff ... @Transactional @SuppressWarnings("unchecked") @Override public void remove(B businessObject) { logger.debug("Remove " + getBusinessObjectCanonicalName() + " id= " + businessObject.getId()); try { getDao().remove(businessObject.getId()); //DOES NOT RECOGNIZE REMOVE METHOD } catch (Exception e) { logger.error("Unable to delete " + getBusinessObjectCanonicalName() + " record id=" + businessObject.getId(), e); } } //stuff ... } </code></pre> <p>Whats the cleanest way to inject a service within this abstract using generics?</p> <p><strong>D extends AbstractJpaBaseDaoImpl</strong> aint cutting it. Is there a pattern i can follow?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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