gpt4 book ai didi

java - 无法在泛型类中调用实体的保存

转载 作者:行者123 更新时间:2023-12-02 07:15:56 24 4
gpt4 key购买 nike

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;
}
}

最佳答案

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

dao.save(entity)

作为“M”

entity = (M) dao.save(entity); 

关于java - 无法在泛型类中调用实体的保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14945791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com