1

How can I replace (or get result with null value) null value in HQL? In my case the SomeObject can be null. I have relation Many-To-One between Clazz and SomeObject:

SELECT c.name, c.someObject.name from Clazz c

I've tried:

SELECT c.name, coalesce(c.someObject.name, ' ') from Clazz c 

but it doesn't return any row.

1
  • 'many'-side is Clazz, 'one'-side is SomeObject and it's value can be null Commented Dec 17, 2012 at 19:58

2 Answers 2

2

You may want to note that there is no such thing as a 'many-to-null' relation. If the referenced object (SomeObject) is null when persisting a Clazz entity no entry will be made into the SomeObject database table.

Then, when you try to access the (non-existent) SomeObject in your query (c.someObject.name) Hibernate will implicitly build an inner join, which of course will exclude the Clazz object in question because the join cannot be fulfilled.

Try to explicitly use an outer join; this will solve your problem.

By the way: You can let hibernate output the SQL statements it generates, which allows to better understand why a given query behaves in a certain way.

Sign up to request clarification or add additional context in comments.

Comments

0

c.someObject.name is an implicit inner join between Clazz and SomeObject. So, since it's an inner join, it automatically filters out Clazz instances having a null someObject.

You need an outer join:

select c.name, s.name from Clazz c
left join c.someObject s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.