Search code examples
javahibernatejpahqljpql

Query in Hibernate with HQL


I have been trying for a long time to make a simple query with HQL but it always return me

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: 
unexpected token: on near line 1, column 72 
[select p, g.Description from entity.TB_Person p inner join TB_Gender g 
 on p.IdGender = g.Id where p.Username= :Username]

Here is the code for the query:

String jpql = "select p, g.Description "
            + "from TB_Person p inner join TB_Gender g "
            + "on p.IdGender = g.Id where p.Username= :Username";
Query query = manager.createQuery(jpql);
query.setParameter("Username", credentials.getUsername());
List l = (List)query.getResultList();

Here are my entities:

package entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name="TB_Person")
public class TB_Person {

@Id
private String CPF;

private String Name;

private Date Birthday; 

private String PhotoPath;

@ForeignKey(name = "fk_TB_Person_TB_User1")
private String Username;

@ForeignKey(name = "fk_person_gender")
private int IdGender;
public String getUsername() {
    return Username;
}

public void setUsername(String username) {
    Username = username;
}

public String getCPF() {
    return CPF;
}

public void setCPF(String cPF) {
    CPF = cPF;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public Date getBirthday() {
    return Birthday;
}

public void setBirthday(Date birthday) {
    Birthday = birthday;
}

public String getPhotoPath() {
    return PhotoPath;
}

public void setPhotoPath(String photoPath) {
    PhotoPath = photoPath;
}
}

package entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="TB_Gender")
public class TB_Gender {

    @Id
    private int Id;

    private String Description;

    private String Abbreviation;

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getAbbreviation() {
        return Abbreviation;
    }

    public void setAbbreviation(String abbreviation) {
        Abbreviation = abbreviation;
    }

    public int getId() {
        return Id;
    }

}

What am I doing wrong?


Solution

  • I think that you are not establishing the relationships between entities correctly.

    @ForeignKey represents a constraint but does not represent the relationship itself between java entities.

    Until Hibernate 5.1 version, you can not use join between entities in JPQL if you do not have established previously a relation between them using annotations like @OneToOne, @OneToMany, @ManyToOne, ... More info about this subject and how to join unrelated entities.

    To execute your query, you have to define something like this in your entities

     @Entity
     @Table(name="TB_Person")
     public class TB_Person {
    
         @Id
         private String CPF;
    
         @OneToOne
         @JoinColumn(name = "user_id") // <- table column constrained by fk_TB_Person_TB_User1
         private TB_User user;
    
         ...
     }