如何在hibernate envers中查询相关实体?

时间:2015-04-09 07:09:07

标签: java hibernate-envers

我有两个实体OrderCustomerOrder

@Entity
@Table(name = "orders")
@Audited(targetAuditMode = RelationTargetAuditMode.AUDITED)
public class Order {

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


    @Audited(withModifiedFlag = true)
    private Double price;

    @Temporal(TemporalType.TIMESTAMP)
    private Calendar createdDate;

    @OneToOne
    private Customer customer;

    public Order() {
    }

    ...................

Customer实体:

@Entity
@Audited
public class Customer {

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

    private String fullname;

    public Customer() {
    }

现在,我想创建一个查询,选择Order已提供customer fullname的{​​{1}}。现在我创建了这样的查询:

auditReader.createQuery().forRevisionsOfEntity(Order.class, true, true).add(AuditEntity.property("customer.fullname").eq("Jhon Smith")).getSingleResult();

但它引发了一个例外:

org.hibernate.QueryException: could not resolve property: customer of: org.baeldung.HibernateAuditDemo.model.Order_AUD [select e__ from org.baeldung.HibernateAuditDemo.model.Order_AUD e__ where e__.customer.fullname = :_p0 order by e__.originalId.REV.id asc]
        at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)

1 个答案:

答案 0 :(得分:0)

您可以使用 mappedBy 属性来映射一对一关系。

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


    .....

    @OneToOne(mappedBy="orders")
    private Customer customer;

    ......




 @Entity
 @Audited
 public class Customer {

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

    private String fullname;

    @OneToOne
    @JoinColumn(name = "my_order")
    private Order order;
    ....

...