org.hibernate.QueryException:无法解析属性:createdDate_MOD:org.baeldung.HibernateAuditDemo.model.Order_AUD

时间:2015-04-08 16:18:39

标签: java hibernate-envers

我使用hibernate envers进行审核。

我有简单的Order实体:

@Entity
@Table(name = "orders")
@Audited
public class Order {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private Long id;

    private Double price;

    @Temporal(TemporalType.TIMESTAMP)
    private Calendar createdDate;

    public Order() {
    }
    //the rest is unimportant

我使用AuditorReader来查询属性已更改的修订:

    AuditReader auditReader = AuditReaderFactory.get(entityManager);
            // find revision at which order entity changed its property createdDate
            List revisions = auditReader.createQuery().forRevisionsOfEntity(Order.class, false, true).
            add(AuditEntity.property("createdDate").hasChanged()).getResultList();

            Object[] history = (Object[]) revisions.get(0);

在这段代码中我得到了异常:

org.hibernate.QueryException: could not resolve property: createdDate_MOD of: org.baeldung.HibernateAuditDemo.model.Order_AUD [select e__, r from org.baeldung.HibernateAuditDemo.model.Order_AUD e__, org.hibernate.envers.DefaultRevisionEntity r where e__.createdDate_MOD = :_p0 and e__.originalId.REV.id = r.id order by e__.originalId.REV.id asc]
        at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
        at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
        at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1967)

在表ordersorders_aud的数据库中,我看到的列名为createdDate,但不是createdDate_MOD

为什么无法解析财产createdDate

2 个答案:

答案 0 :(得分:0)

告诉hibernate envers跟踪您需要在该属性上设置@Audited(withModifiedFlag = true)的某些属性的更改。

要为所有实体全局设置,需要在hibernate属性中将org.hibernate.envers.global_with_modified_flag设置为true。

答案 1 :(得分:0)

Volodymir的答案完美地说明了调用AuditEntity.property("some_property").hasChanged()方法需要做什么。

但是,有些人在不使用hasChanged方法时可能会遇到同样的神秘异常,就像我一样。

对于此设置:

@Entity(name = "prnt")
@Audited
public class ParentClass {
    @Id private Long id;
}

@Entity(name = "chld")
@Audited
public class ChildClass {
    @Id private Long id;

    @ManyToOne @JoinColumn(name = "shrt_prnt_id") 
    private ParentClass looongishLinkToParentClass;
}

...人们可能希望它能起作用:

AuditQuery q = ...;
q.add(AuditEntity.property("shrt_prnt_id")).eq(value));

..或至少是这样:

q.add(AuditEntity.property("looongishLinkToParentClass")).eq(value));

这些都不起作用!

此操作可以完成

q.add(AuditEntity.property("looongishLinkToParentClass_id")).eq(value));