gpt4 book ai didi

hibernate - org.hibernate.QueryException : not an association

转载 作者:行者123 更新时间:2023-12-03 22:47:38 27 4
gpt4 key购买 nike

我想通过限制在 Hibernate 中的条件添加订单。
我有两个表 Event 和 MainEvent 与一对多的关系(一个 MainEvent 有很多事件)。

    final Criteria criteria = getCurrentSession().createCriteria(
Event.class);
criteria.createAlias("mainEvent.date", "deadline");
criteria.addOrder(Order.asc("deadline"));
return criteria.list();

当我尝试运行它时,它给了我以下错误:

org.hibernate.QueryException:不是关联:日期。

主事件实体:
@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;

}

最佳答案

根据 Criteria.createAlias 的 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)

关于hibernate - org.hibernate.QueryException : not an association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com