如何从实体本身分离一个侦听hibernate事件的模块? [英] How to decouple a module which listens on a hibernate event from the entities themselves?

查看:107
本文介绍了如何从实体本身分离一个侦听hibernate事件的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由spring-jpa-hibernate驱动的分层web应用程序,现在我正试图整合elasticsearch(搜索引擎)。

我想要做的是捕获所有postInsert / postUpdate事件,并将这些实体发送到elasticsearch,以便重新对它们进行索引。



我遇到的问题是我的dal-entities项目将对运行时依赖于search-indexer和search-indexer对dal-entities有编译依赖性,因为它需要为不同的实体做不同的事情。

我曾想过将search-indexer作为DAL的一部分(因为它可以被认为是对数据进行操作),但即使如此,它仍然应该是DAO部分的一部分。

我认为我的问题可以改写为:我如何在一个hibernate事件监听器中有逻辑,这个监听器不能仅仅封装在实体项目中(因为它不是它的责任) 。

更新

dal-entities项目依赖于索引器的原因是我需要配置监听器在负责jpa上下文(明显驻留在dal实体中)的spring配置文件中。
依赖不是编译时作用域,而是运行时作用域(因为在运行时hibernate上下文将需要这个监听器)。

解决方案

答案是Interfaces。

不是直接依赖各种类(在任何方向上),而是依赖于表示所需功能的接口。这样,你不直接依赖于类,而是依赖于接口,并且你可以让dal-entities所需的接口,例如,与dal-entities和索引器只是简单地实现了这个接口。



这并不完全删除依赖关系,但它 em>给你一个耦合少得多,让你的应用程序更灵活一些。



如果你仍然担心事情的存在太紧密耦合,或者如果你真的不希望这两个部分完全依赖循环,那么我建议你重新考虑你的应用程序设计。在这里提出另一个关于SO的问题,关于你的一些代码的更多细节以及如何更好地构造它们可能会对如何改进设计提供一些很好的建议。


I have a layered web-application driven by spring-jpa-hibernate and I'm now trying to integrate elasticsearch (search engine).

What I Want to do is to capture all postInsert/postUpdate events and send those entities to elasticsearch so that it will reindex them.

The problem I'm facing is that my "dal-entities" project will have a runtime dependency on the "search-indexer" and the "search-indexer" will have a compile dependency on "dal-entities" since it needs to do different things for different entities.

I thought about having the "search-indexer" as part of the DAL (since it can be argued it does operations on the data) but even still it should be as part of the DAO section.

I think my question can be rephrased as: How can I have logic in a hibernate event listener which cannot be encapsulated solely in an entities project (since it's not its responsibility).

Update
The reason the dal-entities project is dependant on the indexer is that I need to configure the listener in the spring configuration file which is responsible for the jpa context (which obviousely resides in the dal-entities).
The dependency is not a compile time scope but a runtime scope (since at runtime the hibernate context will need that listener).

解决方案

The answer is Interfaces.

Rather than depend on the various classes directly (in either direction), you can instead depend on Interfaces that surface the capabilities you need. This way, you are not directly dependent on the classes but instead depend on the interface, and you can have the interfaces required by the "dal-entities", for example, live in the same package as the dal-entities and the indexer simply implements that interface.

This doesn't fully remove the dependency, but it does give you a much less tight of a coupling and makes your application a bit more flexible.

If you are still worried about things being too tightly coupled or if you really don't want the two pieces to be circularly dependent at all, then I would suggest you re-think your application design. Asking another question here on SO with more details about some of your code and how it could be better structured would be likely to get some good advice on how to improve the design.

这篇关于如何从实体本身分离一个侦听hibernate事件的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆