[英]Hibernate Invalid Path Exception
New to Hibernate and HQL in general.一般而言,Hibernate 和 HQL 是新手。 I have these following entities.
我有以下这些实体。
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private Integer authority;
@Column
private String username;
@Column(name = "user_password")
private String password;
@Column
private String title;
@OneToOne
@JoinColumn(name = "person_id", referencedColumnName = "id")
private Person person;
@Column
private Integer gender;
@Column
private String ssn;
@Column(name = "car_info")
private String carInfo;
@Column(name = "birth_date")
private Date birthDate; //java.util Date vs java.sql.Date?
@OneToOne
@JoinColumn(name = "visa_status_id", referencedColumnName = "id")
private VisaStatus visaStatus;
@Column(name = "license_number")
private Integer licenseNumber;
@Column(name = "license_expiration_date")
private Date licenseExpirationDate;
@ManyToOne
@JoinColumn(name = "housing_id", referencedColumnName = "id")
private House house;
}
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "middle_name")
private String middleName;
@Column(name = "preferred_name")
private String preferredName;
@ManyToOne
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
@Column
private String email;
@Column(name = "ceil_phone")
private String ceilPhone;
@Column(name = "work_phone")
private String workPhone;
}
public class House implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
@ManyToOne
@JoinColumn(name = "contact_id")
private Contact contactHR;
@ManyToOne
@JoinColumn(name = "landlord_id")
private Person landlord;
}
I'm trying to run this HQL:我正在尝试运行此 HQL:
select person from Employee where house.id=:houseId
I'm basically trying to get all the Person
with the houseId
from the Employee
.我基本上是想从
Employee
中获取所有具有houseId
的Person
。 Since I'm trying to only get the person, I'm using the select
cause.因为我只想找到那个人,所以我使用了
select
原因。
But I'm getting these errors:但我收到这些错误:
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'house.id'
org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'house.id' [select person from example.project.domain.entity.Employee where house.id=:houseId]
I tried the HQL without the select
clause and it worked:我尝试了没有
select
子句的 HQL,它起作用了:
from Employee where house.id=:houseId
But I only need person
so I didn't want to get everything.但我只需要
person
,所以我不想得到一切。 Any idea what's wrong?知道出了什么问题吗?
You asked您询问
Hibernate Invalid Path Exception using HQL
使用 HQL 的Hibernate无效路径异常
The problem lies on the written query问题出在书面查询上
select person from Employee where house.id=:houseId
Which should contain proper referencing其中应包含适当的引用
select p.person from Employee p where p.house.id=:houseId
Why?为什么?
The FROM
clause defines which entities the data is going to be selected. FROM
子句定义数据将被选择的实体。 Hibernate, or any other JPA implementation, maps the entities to the according database tables and the syntax of a JPQL FROM
clause is similar to SQL and indeed uses the entity model for referencing database attributes, which in root query execution (same thing you are doing) one must use the same referencing strategy, which in your case is to use aliases, as A.Panfilov said in comments regardless of hibernate, even in aliasing in SQL is a good practice. Hibernate 或任何其他 JPA 实现将实体映射到相应的数据库表,并且 JPQL
FROM
子句的语法类似于 SQL 并且确实使用实体模型来引用数据库属性,这在根查询执行中(与您正在做的一样) ) 必须使用相同的引用策略,在您的情况下就是使用别名,正如A.Panfilov在评论中所说的那样,无论休眠如何,即使在 SQL 中使用别名也是一种很好的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.