2013-02-18 40 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