2010-11-12 64 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

相关问题