Hibernate 中的通用 DAO 模式
在处理 Hibernate 时,我们遵循 Hibernate Doc 中提到的通用 Hibernate DAO 模式。
因此,据此,我们目前维持两个平行的等级制度 1) 对于接口 2)对于实现,
因此如果我们以这种方式工作,即使除了标准持久性方法之外没有提出新方法,我们也需要为该条目及其实现创建一个标记接口。
尽管这种方法及其明确的分离似乎没有问题。
我的问题是是否有更好的方法/替代方法来实现此目的
提前致谢
While working on hibernate we are following generic Hibernate DAO pattern as mentioned in Hibernate Doc also.
So as per this we are currently maintaining two parallel hirarchies
1) for interfaces
2) for Implimentation
so if we work on this the way even if there is no proposed new method beside standard persistannce methods we need to create a marker interface for that entiry as well its Implimentation.
Though there seems no problem in this approach and its clear seperation.
my question is if there any better way/alternate way to achieve this
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Umesh 我将向您展示如何实现此功能
接口
因为您通常不需要上面显示的所有方法,所以我们创建一个抽象类其目的是作为虚拟实现 >
现在,例如,如果您想要一个仅需要添加方法的存储库,您可以使用
如果其他开发人员尝试访问除添加方法之外的其他方法,他或她将得到UnsupportedOperationException
Criteria 只是标记界面。
某些方法定义参数类 fetchingStrategy 的目的是为了匹配外部化命名查询。这样,我就避免了容易出错的手工编码字符串。例如,JSR-303 bean 验证使用此方法来验证组属性。请参阅 这里
外部化命名查询如下所示
所以当我想检索所有具有地址的人时,我调用
findAll 可以写为
Umesh I will show you how we implement this functionality
The interface
Because you usually will not need all of methods shown above, we create an abstract class with the purpose of being a dummy implementation
Now, for instance, if you want a repository which needs only add method, you can use
If other developer try to access other than add method, he or she will get UnsupportedOperationException
Criteria is just a marker interface.
The purpose of some methods define a parameter Class fetchingStrategy is to match externalized named queries. This way, I avoid hand-coded string which is error-prone. This approach is used by JSR-303 bean validation, for instance, to validate groups of properties. See here
The externalize named query is shown as follows
So when i want to retrieve all of person with address, i call
findAll can be written as