问题描述
我正在设计数据库,但遇到了实体本身需要访问数据库的情况.
I'm designing my database and I reached the situation that an Entity itself requires to access database.
我阅读了为什么有时需要在JPA实体内部引用EntityManager .
我很好奇在EntityListener中访问EJB的可能性.
And I'm curious about the possibility of accessing an EJB in EntityListener.
public class MyEntityListener {
@PrePersist
private void onPrePersist(final Object object) {
// find an EJB
// and set those required values
// which each resides in a specific table.
}
}
这是首选还是(或)和?
Is this possible or (or and) preferred?
推荐答案
我找到了答案.
- CDI injection in EntityListeners
- https://blogs.oracle.com/arungupta/entry/jpa_2_1_early_draft
根据 JSR 338:JavaTM Persistence 2.1 ,这似乎是可能的.
It seems possible, according to JSR 338: JavaTM Persistence 2.1.
3.5.1实体侦听器
持久性提供程序负责使用CDI SPI创建实体侦听器类的实例;在这种情况下进行注射;调用其PostConstruct
和PreDestroy
方法(如果有);并处理实体侦听器实例.
The persistence provider is responsible for using the CDI SPI to create instances of the entity listener class; to perform injection upon such instances; to invoke their PostConstruct
and PreDestroy
methods, if any; and to dispose of the entity listener instances.
这篇关于如何在EntityListener中使用EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!