2010-11-12 57 views
0

第一次做泛型,我在這裏有點困惑。類型GenericDao <Order,capture#2-of?>中的方法read(capture#2 of?)不適用於參數(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()讀取(PID)PID是通過對服務類的長期負載方法。

+0

你有OrderDao的代碼嗎? – Pace 2010-11-12 04:54:56

+0

<豆ID = 「orderDao」 類= 「vsg.ecotrak.framework.dao.GenericDaoHibernateImpl」> <構造精氨酸> vsg.ecotrak.common.order.domain.Order <屬性名=「SessionFactory的」> boyd4715 2010-11-12 05:00:35

+0

你應該標題改成你的問題有點像「我如何獲得仿製藥在一個Spring上下文中工作嗎?」然後在您的問題文本中包含例外。這將使人們更容易找到以後。 – 2010-11-12 07:49:38

回答

2

問題在於orderDao的春季宣言。你寫它的方式,這將被Spring解釋爲

new GenericDaoHibernateImpl(Order something) 

,而仿製藥需要這樣的簽名(去除不必要的構造函數參數)。

new GenericDaoHibernateImpl<Order,Long>() 

你不能直接從春推斷仿製藥由於在運行時類型擦除,但你可以創建一個新類

public class OrderDao extends GenericDaoHibernateImpl<Order,Long> { } 

,並引用它,因爲它是自己的豆在Spring

<bean id="orderDao" class="vsg.ecotrak.framework.dao.OrderDao"> 
    <property name="sessionFactory"> <ref bean="sessionFactory"/> 
</bean> 

該泛型包含在OrderDao中,其行爲與僅基於Long PK的返回訂單相同。

+0

謝謝。我試圖避免始終需要創建DAO對象。我最終從通用中刪除了PK,並直接使用Long數據類型。 – boyd4715 2010-11-12 13:32:16

+1

@ boyd4715很高興能幫到你。另外,您可能想要發佈解決方案,以便其他人可以從中受益。接受你自己的答案也沒有什麼壞處。 – 2010-11-12 13:37:34

相關問題