Andrew
Andrew

Reputation: 419

jpql, fetching row with nullable variable

@Entity
class A{
   @OneToOne(optional=true)
   @JoinColumn(foreignKey = @ForeignKey(name = "FK_USER"), nullable = true)
   B b;
   ..other fields...
}

@Entity 
class B{
   ....
}

I want to fetch rows of table A. I'm making jpql request

select new ADTO(b,..other fields(don't necessarily all)...) from A 

(ADTO - data transfer object)

And it's works, but just when A::b not null. If A::b is null this row doesn't selects.

And if i Have another request, it works good, even if rows content null A::b

select new ADTO(..filds without A::b...) from A 

How can i select row with nullable A::b?

Upvotes: 1

Views: 43

Answers (1)

Mathias Begert
Mathias Begert

Reputation: 2471

To not getting confused with aliases and entity names, I rename your Entities A and B to Parent and Child then it would be queried with an outer join like this:

select new ParentDTO(c, p.otherfield) from Parent p left join p.child c

Upvotes: 1

Related Questions