Java 无法在泛型类中调用实体上的保存
在IntellijIDEA中,您可以将光标放在错误上,然后使用ALT+ENTER,intellij可能会建议您转换错误的结果Java 无法在泛型类中调用实体上的保存,java,generics,service,dao,Java,Generics,Service,Dao,在IntellijIDEA中,您可以将光标放在错误上,然后使用ALT+ENTER,intellij可能会建议您转换错误的结果 public class GenericService<T, M, Dao extends GenericDao> { protected Dao dao; protected EntityManager em; public GenericService() { } //map the dao/entity m
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;
}
}
作为“M”
您应该使
GenericDao
也使用泛型:
entity = (M) dao.save(entity);
类GenericDao{
公共M保存(M实体){
...
}
}
然后按如下方式扩展您的通用服务:
class GenericDao<M> {
public M save(M entity) {
...
}
}
公共类泛型服务{
Dao/GenericDao的代码是什么?“这实际上比我提出的答案要好。我只想介绍一些intellij黑客技巧。
entity = (M) dao.save(entity);
class GenericDao<M> {
public M save(M entity) {
...
}
}
public class GenericService<T, M, Dao extends GenericDao<M>> {