gpt4 book ai didi

java - 如何通过复合对象属性查找?

转载 作者:行者123 更新时间:2023-12-02 03:36:44 24 4
gpt4 key购买 nike

我想查找城市名称等于 Madrin 的所有计划(例如)。但我总是收到错误。

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.hibernate.QueryException:无法解析属性:location.lName of:io.onek.entity.FuturePlan。

帮我想想办法。

future 计划

@Entity
public class FuturePlan {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int fpId;
private String about;
@DateTimeFormat(pattern = "dd-mm-yyyy")
@Temporal(TemporalType.DATE)
private Date travelDate;
@ManyToOne(fetch=FetchType.EAGER)
private User user;
@ManyToOne(fetch=FetchType.EAGER)
private Location location;

public FuturePlan() {
super();
}

public FuturePlan(String about, Date travelDate) {
super();
this.about = about;
this.travelDate = travelDate;
}

public FuturePlan(String about, Date travelDate, User user, Location location) {
this.about = about;
this.travelDate = travelDate;
this.user = user;
this.location = location;
}
..get/set/

位置

@Entity
public class Location {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int lId;
private String lName;

public Location() {
super();
}

public Location(int lId, String lName) {
super();
this.lId = lId;
this.lName = lName;
}

来 self 的 DAOImpl 的方法。

@Override
public List<FuturePlan> findByLocation(String name) {
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(FuturePlan.class);
cr.add(Restrictions.eq("location.lName", name));
List<FuturePlan> list = cr.list();
session.close();
return list;
}

最佳答案

您必须创建联接:

cr.createAlias("location", "l"));
cr.add(Restrictions.eq("l.lName", name));

但我真的会避免使用 Criteria 来进行如此简单的静态查询。使用 JPQL:

select fp from FuturePlan fp where fp.location.lName = :name

关于java - 如何通过复合对象属性查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373381/

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