gpt4 book ai didi

spring - 如何在没有事件 session 的情况下将实体监听器添加到 JPA (EclipseLink) 实体?

转载 作者:行者123 更新时间:2023-12-02 11:36:48 25 4
gpt4 key购买 nike

我正在尝试在 spring bean 中执行以下操作:

@PostConstruct
public void registerTorchEntityListeners()
{
Session session = entityManager.unwrap(Session.class);
for (EntityType<?> entity : entityManager.getMetamodel().getEntities())
{
if (entity.getJavaType().isAnnotationPresent(TorchEntityListeners.class))
{
TorchEntityListeners annotation = (TorchEntityListeners) entity.getJavaType().getAnnotation(TorchEntityListeners.class);
for (Class listenerClass : annotation.value())
{
Map<String, DescriptorEventListener> map = applicationContext.getBeansOfType(listenerClass);
for (DescriptorEventListener listenerBean : map.values())
{
session.getClassDescriptor(entity.getClass()).getEventManager().addListener(listenerBean);
}
}
}
}

}

问题是我得到以下异常,因为(我认为)我不在事务中,因此没有可用于获取 ClassDescriptor 的 session ,以便我可以向特定实体添加监听器:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'torchEntityListenerConfigurer': Invocation of init method failed; nested exception is java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:396)

基本上我正在尝试执行与此等效的 EclipseLink:http://invariantproperties.com/2013/09/29/spring-injected-beans-in-jpa-entitylisteners/ 。我更愿意用监听器注释实体,而不是执行如下操作:Injecting a Spring dependency into a JPA EntityListener .

想法?

最佳答案

当然,我在添加赏金后 30 分钟就弄清楚了:)

我最终通过从 EntityManagerFactory 中的连线获取实体管理器(而不是使用:@PersistenceContext 将其注入(inject)到 TorchEntityListenerConfigurer 中)来实现此目的

这是可行的解决方案......而且效果很好!

这是配置:

<bean id="approvalEntityListener" class="com.prometheus.torchlms.core.activity.approval.ApprovalEntityListener">
<property name="activityRepository" ref="activityRepository" />
<property name="notificationFactory" ref="notificationFactory" />
<property name="notificationService" ref="notificationService" />
</bean>
<bean id="springEntityListenerConfigurer" class="com.prometheus.torchlms.core.SpringEntityListenerConfigurer">
<constructor-arg ref="entityManagerFactory" />
</bean>

这就是神奇发生的地方(如果这对某人有用):

public class SpringEntityListenerConfigurer implements ApplicationContextAware
{
private ApplicationContext applicationContext;
private EntityManagerFactory entityManagerFactory;

public SpringEntityListenerConfigurer(EntityManagerFactory entityManagerFactory)
{
this.entityManagerFactory = entityManagerFactory;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException
{
this.applicationContext = applicationContext;
}

@PostConstruct
public void registerTorchEntityListeners()
{
//entityManager.
EntityManager entityManager = entityManagerFactory.createEntityManager();
Session session = entityManager.unwrap(Session.class);
for (EntityType<?> entity : entityManagerFactory.getMetamodel().getEntities())
{
if (entity.getJavaType().isAnnotationPresent(SpringEntityListeners.class))
{
SpringEntityListeners annotation = (SpringEntityListeners) entity.getJavaType().getAnnotation(SpringEntityListeners.class);
for (Class listenerClass : annotation.value())
{
Map<String, DescriptorEventListener> map = applicationContext.getBeansOfType(listenerClass);
for (DescriptorEventListener listenerBean : map.values())
{
ClassDescriptor classDescriptor = session.getClassDescriptor(entity.getJavaType());
if (null != classDescriptor)
{
classDescriptor.getEventManager().addListener(listenerBean);
}
}
}
}
}

}
}

现在我可以将我的 @SpringEntityListeners({ApprovalEntityListener.class}) 添加到我想要的任何实体以及我想要的任何监听器,并且这些监听器可以是 spring beans!

如果它有帮助,这里是注释:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SpringEntityListeners
{
Class<?>[] value();
}

关于spring - 如何在没有事件 session 的情况下将实体监听器添加到 JPA (EclipseLink) 实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25224786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com