Ricardo
Ricardo

Reputation: 1025

How to include NULL using Order By with JPQL?

I am trying to run such JPQL query:

SELECT t1 FROM Table1 t1 ORDER BY t1.column1.id ASC

Column1 as such implementation:

// bi-directional many-to-one association to Table1
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idTable1Parent")
private table1      column1;

Table1 column idTable1Parent is a FK to the Table1 column id PK.

Some records have idTable1Parent as NULL.

The problem is when I ORDER BY ASC the values, the records that have idTable1Parent are not returned, and I need that records.

Do you have some tips or solution so I can get the records that have idTable1Parent as null?

Thanks in advance.

Upvotes: 1

Views: 1775

Answers (1)

James
James

Reputation: 18379

You need to use an outer join,

SELECT t1 FROM Table1 t1 left join t1.column1 c1 ORDER BY c1.id ASC

Upvotes: 2

Related Questions