我正在尝试执行简单的条件,但得到“org.hibernate.QueryException:无法解析属性”。 实际上我必须在ProcessInstanceJPA上应用条件,但它不能识别ExpenseHeaderJPA中的processInstanceJPA。
标准:
List<ExpenseHeaderJPA> expsensHeaderList= session.createCriteria(ExpenseHeaderJPA.class)
.add(Restrictions.eq("processInstanceJPA.processInstanceId", new Long(1))
.list();
ProcessInstaceJPA
@Entity
@Table(name="process_instance")
public class ProcessInstanceJPA {
@Id @GeneratedValue
@Column(name="process_instance_id")
private Long processInstanceId;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="expense_header_id")
/*@OneToOne
@PrimaryKeyJoinColumn*/
private ExpenseHeaderJPA expenseHeaderJPA;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="voucher_status_id")
private VoucherStatusJPA voucherStatusJPA;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="pending_at")
private EmployeeJPA pendingAt;
public Long getProcessInstanceId() {
return processInstanceId;
}
public void setProcessInstanceId(Long processInstanceId) {
this.processInstanceId = processInstanceId;
}
public ExpenseHeaderJPA getExpenseHeaderJPA() {
return expenseHeaderJPA;
}
public void setExpenseHeaderJPA(ExpenseHeaderJPA expenseHeaderJPA) {
this.expenseHeaderJPA = expenseHeaderJPA;
}
public VoucherStatusJPA getVoucherStatusJPA() {
return voucherStatusJPA;
}
public void setVoucherStatusJPA(VoucherStatusJPA voucherStatusJPA) {
this.voucherStatusJPA = voucherStatusJPA;
}
public EmployeeJPA getPendingAt() {
return pendingAt;
}
public void setPendingAt(EmployeeJPA pendingAt) {
this.pendingAt = pendingAt;
}
}
ExpenseHeaderJPA
@Entity
@Table(name="expense_header")
public class ExpenseHeaderJPA {
@Id @GeneratedValue
@Column(name="expense_header_id")
private Long expenseHeaderId;
@OneToMany(orphanRemoval=true,mappedBy="expenseHeaderJPA",fetch=FetchType.EAGER)
@Cascade({CascadeType.ALL})
@Fetch (FetchMode.SELECT)
private List<ExpenseDetailJPA> expenseDetailJPA;
@OneToMany(orphanRemoval=true,mappedBy="expenseHeaderJPA",fetch=FetchType.EAGER)
@Cascade({CascadeType.ALL})
@Fetch (FetchMode.SELECT)
private List<ProcessHistoryJPA> processHistoryJPA;
@OneToOne(orphanRemoval=true,mappedBy="expenseHeaderJPA",fetch=FetchType.EAGER)
@Cascade({CascadeType.ALL})
@Fetch (FetchMode.SELECT)
private ProcessInstanceJPA processInstanceJPA;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private EmployeeJPA employeeJPA;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voucher_status")
private VoucherStatusJPA voucherStatusJPA;
@Column(name="start_date")
private Calendar startDate;
@Column(name="end_date")
private Calendar endDate;
@Column(name="title")
private String title;
@Column(name="purpose")
private String purpose;
public Long getExpenseHeaderId() {
return expenseHeaderId;
}
public void setExpenseHeaderId(Long expenseHeaderId) {
this.expenseHeaderId = expenseHeaderId;
}
public Calendar getStartDate() {
return startDate;
}
public void setStartDate(Calendar startDate) {
this.startDate = startDate;
}
public Calendar getEndDate() {
return endDate;
}
public void setEndDate(Calendar endDate) {
this.endDate = endDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPurpose() {
return purpose;
}
public void setPurpose(String purpose) {
this.purpose = purpose;
}
public List<ExpenseDetailJPA> getExpenseDetailJPA() {
return expenseDetailJPA;
}
public void setExpenseDetailJPA(List<ExpenseDetailJPA> expenseDetailJPA) {
this.expenseDetailJPA = expenseDetailJPA;
}
public EmployeeJPA getEmployeeJPA() {
return employeeJPA;
}
public void setEmployeeJPA(EmployeeJPA employeeJPA) {
this.employeeJPA = employeeJPA;
}
public VoucherStatusJPA getVoucherStatusJPA() {
return voucherStatusJPA;
}
public List<ProcessHistoryJPA> getProcessHistoryJPA() {
return processHistoryJPA;
}
public void setProcessHistoryJPA(List<ProcessHistoryJPA> processHistoryJPA) {
this.processHistoryJPA = processHistoryJPA;
}
public void setVoucherStatusJPA(VoucherStatusJPA voucherStatusJPA) {
this.voucherStatusJPA = voucherStatusJPA;
}
public ProcessInstanceJPA getProcessInstanceJPA() {
return processInstanceJPA;
}
public void setProcessInstanceJPA(ProcessInstanceJPA processInstanceJPA) {
this.processInstanceJPA = processInstanceJPA;
}
}
ExpenseHeaderJPA是父表,ProcessInstanceJPA是子表。它们之间有OnetoOne映射。当我试图对ProcessInstaceJPA的任何属性应用限制时,我收到无效的属性错误。
MySQL数据库结构:
CREATE TABLE `expense_header` (
`expense_header_id` bigint(10) NOT NULL AUTO_INCREMENT,
....
....
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
CREATE TABLE `process_instance` (
`process_instance_id` bigint(10) NOT NULL AUTO_INCREMENT,
`expense_header_id` bigint(10) NOT NULL,
...
...
)
请帮助解决此问题。
答案 0 :(得分:0)
我根本不了解你想要做什么,但似乎你想要一个加入,而不是一个平等。
我认为Restrictions类没有那个选项,你可以使用CriteriaBuilder来实现。
您可以这样做:
//you should initialize this variables depending on how you are working with hibernate
private CriteriaBuilder criteriaBuilder;
private CriteriaQuery<ExpenseHeaderJPA> criteria;
private Root<ExpenseHeaderJPA> root;
private Predicate exp;
exp.getExpressions().add(criteriaBuilder.equal(root.join(processInstanceJPA).get(processInstanceId), new Long(1)));
this.criteria.where(exp);
return session.createQuery().getResultList(); //I don't know what you have in session, but you should be able to create the query.