我有两个实体Order
和Customer
。 Order
是
@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)
答案 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;
....
...