Saturday, 10 May 2014

java.lang.IllegalStateException: DOT node with no left-hand-side!

This is my account of programming problems/exceptions and their solutions that I found by googling or debugging.
This may also include some of the projects that I have implemented in order to learn some new technologies.

While using JPA with hibernate 4.3.0.Final encountered following exception:

   java.lang.IllegalStateException: DOT node with no left-hand-side!

Exception occured when I tried to use Left Join in JPA query.
As I was trying to avoid any native dependencies using pure JPA, I could not use native queries.

It's strange that it worked when I used inner join (without) actually mentioning it in the query.
Everywhere I googled it was suggested that there can not be a join in Hibernate if the entities joined did not have any relationship.

But in my case I had a relationship. As it can be seen below
@Entity
@Table(name = "country")
public class CountryEntity {
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "countryId", nullable = false, unique = true)
 private long countryId;
}

@Entity
@Table(name = "capital")
public class CapitalEntity {
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "capitalId", nullable = false, unique = true)
 private long capitalId;
 
 @ManyToOne
 @JoinColumn(name = "country")
 CountryEntity country;
}
For brevity getters and setters are not included.

Following Query works

Query 1

select c, cap from
CountryEntity c, Capital cap
where cap.country = c
After including left join it doesn't work anymore

Query 2

select c, cap from
CountryEntity c left join Capital cap
where cap.country = c

Solution

Use following query

Query 3

select c, cap from
Capital cap right join cap.country 
Don't use the condition now or it will again force the inner join.

 What went wrong ?

As I was using hibernate as JPA implementation and it doesn't support join between unrelated entities, Query 2 was interpreting Country and Capital as two unrelated entities.
When in Query 3 the relationship was explicitly specified by using cap.country it worked.

How I came up with Query 3?

 see here


...till next time.