1

I have these following Classes:

class Person(){
   @OneToMany(mappedBy="person")
   private List<PersonRoles> roles;
}

class PersonRoles(){
   @ManyToOne
   @JoinColumn(name = "person_id", nullable = false)
   private Person person;

   @ManyToOne
   @JoinColumn(name = "request_id")
   private Request request;
}

class Request(){
   @OneToMany(mappedBy="request")
   private List<PersonRoles> roles;
}

Now I am going to fetch all person based on a given request id and his roles by using hibernate and inner join but my log is telling me that my table doesn't exist. This is my query so far:

sql = "SELECT p.* FROM person AS p INNER JOIN p.roles ON p.roles.personId = p.id 
INNER JOIN request AS r ON p.roles.requestId = r.id AND p.roles.role like :item 
AND r.id = :id";
query = session.createSQLQuery(sql);
query.addEntity(Person.class);
query.setParameter("item", "Members");
query.setParameter("id", id);
person = (Person) query.uniqueResult();

and this is what i received on the log:

Table 'p.roles' doesn't exist

Did i forget some hibernate annotation? or My query has something wrong?

Anirudh Lou
  • 781
  • 2
  • 10
  • 28

1 Answers1

0

Brief reason

your syntax of SQL is wrong

Detailed explanation

here is the syntax of inner join example

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

for multiple inner join

SELECT * 
FROM table1 
INNER JOIN table2
      ON table1.primaryKey=table2.table1Id
INNER JOIN table3
      ON table1.primaryKey=table3.table1Id

but you have used INNER JOIN p.roles there should be a table name after the INNER JOIN, not a column name.

that's why you got an error, moreover, use HQL instead of SQL in hibernate it is a good practice.

happy coding!

majurageerthan
  • 2,169
  • 3
  • 17
  • 30