NullPointerException 在 org.hibernate.envers.event.AuditEventListener.generateBi DirectionCollectionChangeWorkUnits(...)

发布于 2024-08-12 07:41:01 字数 1688 浏览 7 评论 0原文

我正在使用 Envers 来审计我的实体的不同字段。该框架总体工作正常,但似乎在某些类型的实体映射方面存在问题。以前的所有问题我都可以自己解决……但这次我陷入了困境。

当将一些实体插入数据库时​​,我得到以下异常:

Caused by: java.lang.NullPointerException
    at org.hibernate.envers.event.AuditEventListener.generateBidirectionalCollectionChangeWorkUnits(AuditEventListener.java:108)

我不完全确定哪个实体导致了这种情况,因为它是在flush()期间触发的,并且复杂的应用程序在一个较大的事务中插入了许多不同的实体。

我们正在使用一些在该异常之前触发的 HibernateEventListener...所以我认为该实体是原因。 persistence.xml 是这样配置的:

        <property name="hibernate.ejb.event.post-insert" value="com.xyz.hibernate.events.listeners.MyListener,org.hibernate.envers.event.AuditEventListener" />

如果这是真的,那么实体如下(摘录):

@Entity
@Table(name = Property.TABLE_NAME, uniqueConstraints = @UniqueConstraint(columnNames = { "ENTITY_ID", "DESCRIPTOR_ID", "PROMOLEVEL_ID" }))
public class Property extends AbstractEntity {
private static final long serialVersionUID = 1L;

public static final String TABLE_NAME = "E_BUSINESS_PROPERTIES";
public static final String PROPERTY_ENTITY = "entity";
public static final String PROPERTY_DESCRIPTOR = "descriptor";
public static final String PROPERTY_PROMOLEVEL = "promolevel";

@Audited
@ManyToOne(optional = false)
private ProjectPropertyDescriptor descriptor;

@Audited
@ManyToOne
private ExtendedEntity entity;

@Audited
@ManyToOne
private AbstractPromotionLevel promolevel;

@Audited
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = PropertyValue.PROPERTY_PROPERTY)
private List<PropertyValue> propertyValues = new ArrayList<PropertyValue>();

// some accessors stripped!

} 

有人知道在哪里寻找吗?一旦我们禁用恩弗斯,一切就正常了。但我们需要时间来生成变化的历史。

I'm using Envers to audit different fields of my entities. The framework works in general but it seems to have problems with some kinds of entity mapping. All former problems I could solve myself... but this time I'm stuck.

When inserting some entity into the database I get the following exception:

Caused by: java.lang.NullPointerException
    at org.hibernate.envers.event.AuditEventListener.generateBidirectionalCollectionChangeWorkUnits(AuditEventListener.java:108)

I'm not completly sure which entity causes this because it is fired during flush() and the complex application inserts many different entities within one larger transaction.

We are using some HibernateEventListener that fires right before that exception... so I suppose that entity is the cause. The persistence.xml is configured this way:

        <property name="hibernate.ejb.event.post-insert" value="com.xyz.hibernate.events.listeners.MyListener,org.hibernate.envers.event.AuditEventListener" />

If this is true than the entity is the following (excerpt):

@Entity
@Table(name = Property.TABLE_NAME, uniqueConstraints = @UniqueConstraint(columnNames = { "ENTITY_ID", "DESCRIPTOR_ID", "PROMOLEVEL_ID" }))
public class Property extends AbstractEntity {
private static final long serialVersionUID = 1L;

public static final String TABLE_NAME = "E_BUSINESS_PROPERTIES";
public static final String PROPERTY_ENTITY = "entity";
public static final String PROPERTY_DESCRIPTOR = "descriptor";
public static final String PROPERTY_PROMOLEVEL = "promolevel";

@Audited
@ManyToOne(optional = false)
private ProjectPropertyDescriptor descriptor;

@Audited
@ManyToOne
private ExtendedEntity entity;

@Audited
@ManyToOne
private AbstractPromotionLevel promolevel;

@Audited
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = PropertyValue.PROPERTY_PROPERTY)
private List<PropertyValue> propertyValues = new ArrayList<PropertyValue>();

// some accessors stripped!

} 

Does anyone have an idea where to look for? As soon as we disable Envers everything works fine. But we need envers to generate a history of changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-08-19 07:41:01

我找到了我的问题的解决方案。所以我会分享给其他人。

对 ExtendedEntity 的引用导致了问题。 ExtendedEntity 是一个具有不同子类的审计类。但 Envers 不会自动将子类标记为已审核。子类必须对类或任何自己的字段使用 @Audited 注释,以供 Envers 审核。

因此,对任何扩展实体子类的引用(经过审计)都有效。就我而言,我引用了另一个未经 Envers 审核的子类 - 因此抛出了 NullPointerException。通过简单地将 @Audited 注释添加到 ExtendedEntity 类的空扩展(没有自己的属性...只是一个子类来区分另一种类型的实体)并在数据库中创建相关版本控制表,我可以缩小这个差距并解决我的问题问题。

请记住在任何自己的字段或类本身上使用 @Audited 标记子类 - 否则它们不会被审核,您可能会遇到完全相同的问题。

I found the solution of my problem. So I'll share it for others.

The reference to the ExtendedEntity caused the problem. ExtendedEntity is an audited class with different subclasses. But Envers does not automatically mark the subclass as audited. The subclass must use the @Audited annotation for the class or any own fields to be audited by Envers.

So a reference to any sublass of ExtendedEntity, that was audited, worked. In my case I've referenced another subclass that was not audited by Envers - and so the NullPointerException was thrown. By simply adding the @Audited annotation to that empty extension of the ExtendedEntity class (no own properties... just a subclass to distinguish another type of entity) and creation of the related versioning table in the database I could close that gap and solve my problem.

Remember to mark subclasses with @Audited at any own field or the class itself - otherwise they aren't audited and you might come across the exact same problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文