2013-02-18 39 views
0
public class JobAssetService extends GenericService<JobAssetService, JobAsset, JobAssetDao> { 

} 

我想为我的服务层提供通用的save()功能,但它似乎不喜欢我传递给dao.save()的东西。这似乎是这应该工作...无法调用通用类内的实体保存

不兼容的类型 要求:M 发现:java.lang.Object继承

public class GenericService<T, M, Dao extends GenericDao> { 

    protected Dao dao; 
    protected EntityManager em; 

    public GenericService() { 

    } 

    //map the dao/entity manager when instantiated 
    public GenericService(Class<Dao> daoClass) { 
     //map entity manager & dao 
     //code removed for readability 
    } 

    public M save(M entity) { 
     EntityTransaction tx = em.getTransaction(); 
     tx.begin(); 
     entity = dao.save(entity); //IntelliJ complains about this 
     tx.commit(); 

     return entity; 
    } 
} 
+0

什么是'dao.save'? – SLaks 2013-02-18 21:45:56

+0

Dao/GenericDao的代码是什么? – mantrid 2013-02-18 21:46:12

回答

1

在IntellijIDEA你可以把你的光标上的错误,然后使用ALT + ENTER和IntelliJ很可能会建议你投的

dao.save(entity) 

为 “M”

entity = (M) dao.save(entity); 
结果
1

你应该做太多GenericDao使用泛型:

class GenericDao<M> { 
    public M save(M entity) { 
     ... 
    } 
} 

然后扩展您的通用服务如下:

public class GenericService<T, M, Dao extends GenericDao<M>> { 
+0

'这实际上比我提出的答案要好。也只是想提示一些intellij黑客。 – 2013-02-18 21:58:49