我正在使用 Hibernate,并且有一个用于我的 Person 实体的数据访问对象:
public interface PersonDAO extends JpaRepository<Person, String>
{
Person findById(String id);
CsFinal findByCsFinal_CsStudentId(String id);
}
我知道我可以创建一个 CsFinal DAO 并只需调用 findByCsStudentId,但我想知道我上面所做的是否可行。这样做的原因是我不确定是否有必要拥有一个 DAO,而整个 DAO 中我只会有一个方法。
编辑:运行当前代码时出现异常:
Servlet.service() for servlet [dispatcherServlet] in context with path []
threw exception [Request processing failed; nested exception is
org.springframework.core.convert.ConverterNotFoundException: No converter
found capable of converting from type [Person] to type [CsFinal]] with
root cause
org.springframework.core.convert.ConverterNotFoundException: No converter
found capable of converting from type [Person] to type [CsFinal]
最佳答案
建议为 CsFinal 使用单独的 Dao,因为当您使用存储库类执行查找全部或保存时,将根据 Person JpaRepository<Person, String>
的主键列完成保存。 。因此,如果您想利用保存功能,理想情况下您需要为每个实体类使用单独的存储库。
关于java - 在类的 DAO 中使用不同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796406/