udaybhaskar
udaybhaskar

Reputation: 169

Not able to get the row if there is null value in one column hibernate hql

I'm having difficulties in search operation. If there is any null value in a column of one row then that row is not coming. I tried using is not null in hql, but I'm not able get the values. I have written hql query like this. It is returning nothing. I'm not able to understand how it is working. Please tell me the solution for this one. I'm using mysql.

session.createQuery("FROM Employee e WHERE (e.company is not null and e.company.name = :p1) or e.name = :p1").setParameter("p1", str)

My @Entity classes are these:

@Entity
public class Employee implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private float salary;

    @OneToOne(cascade = CascadeType.ALL)
    private Company company;

data of <code>Employee</code> in the Database

@Entity
public class Company implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String Location;

data of the <code>Company</code> table

Upvotes: 0

Views: 2870

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24423

Try with this

session.createQuery("FROM Employee e left join e.company c WHERE c.name = :p1 or e.name = :p1").setParameter("p1", "uday11")

When you use e.company in your query it is translated to inner join, that's why it is not working as expected.

Upvotes: 1

Related Questions