Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在泛型类中调用实体上的保存_Java_Generics_Service_Dao - Fatal编程技术网

Java 无法在泛型类中调用实体上的保存

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

在IntellijIDEA中,您可以将光标放在错误上,然后使用ALT+ENTER,intellij可能会建议您转换错误的结果

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>> {