Home > Net >  Hibernate Invalid Path Exception
Hibernate Invalid Path Exception

Time:12-16

New to Hibernate and HQL in general. I have these following entities.

Employee entity

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;
}

Person entity

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;

}

House entity

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:

select person from Employee where house.id=:houseId

I'm basically trying to get all the Person with the houseId from the Employee. Since I'm trying to only get the person, I'm using the select cause.

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:

from Employee where house.id=:houseId

But I only need person so I didn't want to get everything. Any idea what's wrong?

CodePudding user response:

You asked

Hibernate Invalid Path Exception using HQL

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. 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.

You may find more in here.

  • Related