org.hibernate.QueryException:不是关联
我想在Hibernate中将order by restriction添加到条件中。 我有两个表Event和MainEvent,它们的关系是一对多(MainEvent有许多事件) 当我尝试运行此程序时,会出现以下错误: org.hibernate.QueryException:不是关联:日期 主事件实体:org.hibernate.QueryException:不是关联,hibernate,criteria,Hibernate,Criteria,我想在Hibernate中将order by restriction添加到条件中。 我有两个表Event和MainEvent,它们的关系是一对多(MainEvent有许多事件) 当我尝试运行此程序时,会出现以下错误: org.hibernate.QueryException:不是关联:日期 主事件实体: @Entity @Table(name = "MAIN_EVENT") public class MainEventEntity { @Id @GeneratedValue(s
@Entity
@Table(name = "MAIN_EVENT")
public class MainEventEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "MAIN_EVENT_ID")
private int id;
@Column(name = "MAIN_EVENT_DATE")
private Date date;
@OneToMany(mappedBy = "mainEvent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<EventEntity> events = new ArrayList<EventEntity>();
}
@Entity
@Table(name = "EVENT")
public class EventEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EVENT_ID")
private int id;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "MAIN_EVENT_ID", nullable = false)
private MainEventEntity mainEvent;
}
根据的API,您可以仅为关联创建别名,而不是为关联的字段创建别名
createAlias
Criteria createAlias(String associationPath,
String alias)
throws HibernateException
Join an association, assigning an alias to the joined association.
Parameters:
associationPath - A dot-seperated property path
alias - The alias to assign to the joined association (for later reference).
Returns:
this (for method chaining)
发布您的实体代码会很高兴看到我们应该在上下文中做什么,而不是从文档中快速粘贴。
createAlias
Criteria createAlias(String associationPath,
String alias)
throws HibernateException
Join an association, assigning an alias to the joined association.
Parameters:
associationPath - A dot-seperated property path
alias - The alias to assign to the joined association (for later reference).
Returns:
this (for method chaining)
final Criteria criteria = getCurrentSession().createCriteria(
Event.class);
criteria.createAlias("mainEvent", "me");
criteria.addOrder(Order.asc("me.deadline"));
return criteria.list();