B, C and D extends A entity. In B and C I have one attribute "name" and I would like to create a query from A entity but I want to retrieve the attribute "name" if it exists. If it doesn't exist or the attribute has null value, null should be returned.

Using JPQL (with eclipse link 2.5)

SELECT TREAT(a as B).name, TREAT(a as C).name FROM A a

but the SQL generated is this one:

SELECT name, name FROM A WHERE ((DTYPE IN (?)) AND (DTYPE IN (?))))

but it is not working because I'm expecting an OR instead of the AND condition.

Am I wrong ? Do am I missing something ?
Is it correct the current behaviour ?

This seems to be the correct behavior because what you are asking for does not make sense. If a is an instance of C or D, then Treat(a as B) does not exist and TREAT(a as B).name should be filtered from the result. TREAT is meant to filter based on the subclass. B and C should share a parent that has the name attribute if you really want to make use of it efficiently.

There are a few options though, such as a union of 'select b.name from B' and 'select c.name from C'