[英]The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String)
[英]The method read(capture#2-of ?) in the type GenericDao<Order,capture#2-of ?> is not applicable for the arguments (Long)
第一次做泛型,我在這里有點困惑。
我有以下內容:
public interface GenericDao<T, PK extends java.io.Serializable> {
/**
* Retrieve an object that was previously persisted to the database
* using the reference id as primary key
*
* @param id primary key
* @return
*/
public T read(PK id);
}
public class GenericDaoHibernateImpl<T, PK extends java.io.Serializable> implements GenericDao<T, PK>
{
private Class<T> type;
private SessionFactory sessionFactory;
/**
*
*/
public GenericDaoHibernateImpl(Class<T> type)
{
this.type = type;
}
@SuppressWarnings("unchecked")
public T read(final PK id)
{
return (T) getSession().get(type, id);
}
}
<bean id="orderDao" class="vsg.ecotrak.framework.dao.GenericDaoHibernateImpl">
<constructor-arg>
<value>vsg.ecotrak.common.order.domain.Order</value>
</constructor-arg>
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
然后我的服務類只調用this.getOrderDao()。read(pId)其中pId作為Long傳遞給服務類的load方法。
問題就出在春季聲明orderDao
。 你編寫它的方式,它將被Spring解釋為
new GenericDaoHibernateImpl(Order something)
而泛型需要這樣的簽名(刪除不必要的構造函數參數)。
new GenericDaoHibernateImpl<Order,Long>()
由於在運行時類型擦除,您不能直接從Spring推斷泛型,但是您可以創建一個新類
public class OrderDao extends GenericDaoHibernateImpl<Order,Long> { }
並在Spring中引用它作為它自己的bean
<bean id="orderDao" class="vsg.ecotrak.framework.dao.OrderDao">
<property name="sessionFactory"> <ref bean="sessionFactory"/>
</bean>
泛型包含在OrderDao中,該行為僅通過返回基於Long PK的Order即可按預期運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.