【问题标题】:Using a different class in a class's DAO在类的 DAO 中使用不同的类
【发布时间】:2017-12-13 15:13:23
【问题描述】:

我正在使用 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]

【问题讨论】:

  • 1.在整个 DAO 中只有一种方法很好(即使没有方法,只有来自层次结构的方法)。 2.你为什么不试试呢?
  • @Shadov 对不起,我忘了添加我得到的异常。继续编辑帖子。似乎共识是每个实体都有一个 DAO,不管每个实体有多少方法

标签: java spring hibernate dao


【解决方案1】:

建议为 CsFinal 使用单独的 Dao,因为当您使用存储库类进行查找全部或保存时,将根据 Person JpaRepository&lt;Person, String&gt; 的主键列进行保存。因此,如果您想利用保存功能,理想情况下您需要为每个实体类建立一个单独的存储库。

【讨论】:

  • 感谢您的推荐。因此,即使我从不打算对这个特定的表进行事务处理,听起来最好的做法是为每个实体拥有一个 DAO,而不管 DAO 有多少方法或功能。对吗?
  • 是的,设计应该是每个实体都有自己的存储库。
猜你喜欢
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2017-11-16
  • 1970-01-01
相关资源
最近更新 更多