Question

I have the following entity caled Product:

@Id 
@SequenceGenerator(name = "Product_seq", sequenceName = "PRODUCT_SEQ", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "Product_seq")
@Column(name = "ID")
private Long id;

@OneToOne
@JoinColumn(name = "CAS_ID")
private CAS cas;
...

And a CAS entity:

@Id
@SequenceGenerator(name = "CAS_seq", sequenceName = "CAS_SEQ", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "CAS_seq")
@Column(name = "ID", unique = true, nullable = false, scale = 0)
private Long id;

@Column(name = "CAS_NUMBER", length = 6)
private String casNumber;

@Column(name = "CAS_DESCRIPTION")
private String casDescription;
...

In my application, I have the following HQL statement:

select p from Product p where p.approve IS NULL order by p.cas.casNumber desc

The issue here is that p.cas.casNumber can be NULL and I need to return those rows as well. I am not sure how to write the LEFT JOIN on this table using the p.cas.casNumber.

I appreciate any of the help you can provide. Thank you!

Was it helpful?

Solution

You could try as follows:

select p from Product p left join p.cas as a where p.approve IS NULL order by a.casNumber desc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top