Java 如何通过复合对象属性查找?
我想找到所有城市名称等于Madrin的平面图(例如)。 但我总是会出错 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.hibernate.QueryException:无法解析属性:location.lName of:io.onek.entity.FuturePlan 帮我弄清楚 未来计划Java 如何通过复合对象属性查找?,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我想找到所有城市名称等于Madrin的平面图(例如)。 但我总是会出错 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.hibernate.QueryException:无法解析属性:location.lName of:io.onek.entity.FuturePlan 帮我弄清楚 未来计划 @Entity public class FuturePlan { @Id @GeneratedValue(stra
@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;
}
方法从我的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;
}
@覆盖
公共列表findByLocation(字符串名称){
Session Session=sessionFactory.openSession();
Criteria cr=session.createCriteria(FuturePlan.class);
cr.add(Restrictions.eq(“location.lName”,name));
List=cr.List();
session.close();
退货清单;
}
必须创建联接:
cr.createAlias("location", "l"));
cr.add(Restrictions.eq("l.lName", name));
但我真的不会为这样一个简单的静态查询使用条件。使用JPQL:
select fp from FuturePlan fp where fp.location.lName = :name
您必须创建一个联接:
cr.createAlias("location", "l"));
cr.add(Restrictions.eq("l.lName", name));
但我真的不会为这样一个简单的静态查询使用条件。使用JPQL:
select fp from FuturePlan fp where fp.location.lName = :name
HQL
-因为他使用了会话
。这个HQL
产生了一个交叉连接HQL
——因为他使用了会话。这个HQL
产生一个交叉连接。