# @ElementCollection with @Embeddable in native SQL query

**URL:** <https://discourse.hibernate.org/t/elementcollection-with-embeddable-in-native-sql-query/11422>\
**Category:** Hibernate ORM\
**Created:** [May 7, 2025, 3:19pm UTC](https://discourse.hibernate.org/t/elementcollection-with-embeddable-in-native-sql-query/11422 "2025-05-07T15:19:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wojciech\_Nowak](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/wojciech_nowak/32/4002_2.png) [@Wojciech\_Nowak](https://discourse.hibernate.org/u/Wojciech_Nowak)\
**Post date:** [May 7, 2025, 3:19pm UTC](https://discourse.hibernate.org/t/elementcollection-with-embeddable-in-native-sql-query/11422/1 "2025-05-07T15:19:54Z")

</div>

Hi,

I’m struggling with joining and selecting @Embeddable in native query.  
Here is an example:

data model:

```auto
CREATE SEQUENCE person_seq;
CREATE TABLE IF NOT EXISTS person(
    id BIGINT PRIMARY KEY NOT NULL DEFAULT NEXTVAL ('person_seq')
);

CREATE TABLE IF NOT EXISTS person_phones(
    person_id BIGINT NOT NULL,
    phone_number VARCHAR(256) NOT NULL,
    dial_code VARCHAR(10),
    CONSTRAINT FK_PERSON_ID FOREIGN KEY (person_id) REFERENCES person (id)
);

```

ORM:

```auto
@Entity
@Table(name = "person")
public class Person {
    @Id
    private Long id;

    @ElementCollection
    @CollectionTable(name = "person_phones", joinColumns = @JoinColumn(name = "person_id"))
    @Column(name = "phone_number")
    private Set<PhoneNumberWithDialCode> phoneNumbers;
}

@Embeddable
public class PhoneNumberWithDialCode {
    @Column(name = "phone_number")
    private String phoneNumber;

    @Column(name = "dial_code")
    private String dialCode;
}

```

and the query (please don’t ask why we use native here. I just simplified much more complex query to reproduce the issue on simpler case):

```auto
return entityManager.createNativeQuery("" +
                "SELECT {p.*}, {pp.*} FROM person p LEFT JOIN person_phones pp ON p.id = pp.person_id")
                .unwrap(NativeQuery.class)
                .addEntity("p", Person.class)
                .addJoin("pp", "p.phoneNumbers")
                .getResultList();

```

This query ends with NullPointerException:

```auto
java.lang.NullPointerException
	at org.hibernate.loader.custom.sql.SQLQueryParser.resolveProperties(SQLQueryParser.java:229)
	at org.hibernate.loader.custom.sql.SQLQueryParser.resolveCollectionProperties(SQLQueryParser.java:186)
	at org.hibernate.loader.custom.sql.SQLQueryParser.substituteBrackets(SQLQueryParser.java:147)
	at org.hibernate.loader.custom.sql.SQLQueryParser.process(SQLQueryParser.java:64)
	at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:117)
	at org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.createQueryPlan(NativeQueryInterpreterStandardImpl.java:70)
	at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:213)
	at org.hibernate.internal.AbstractSharedSessionContract.getNativeQueryPlan(AbstractSharedSessionContract.java:549)

```

I already debugged it and the problem is as following:  
In `SqlQueryReturnProcessor.addCollection()` method persister is not added to context, as the @ElementCollection is neither one-to-many nor mamy-to-many, so the following `if`-statement is not entered, however then in `SQLQueryParser.resolveProperties()` method, it tries to call `selectFragment()` method on `persister` field which is of course null in this case, which causes NPE:

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/7/716023ef64e143572912d2820dc9fff913b070a3.png)

Any idea what could be wrong here? I don’t know if I’m doing someting wrong or it’s a bug.  
The above code was tested on Hibernate 5.2.4.Final  
Any ideas what could be wrong here?

I also tried following:

```auto
SELECT {p.*}, {pp.element.*} FROM ...

```

same result

```auto
SELECT {p.*}, {pp.element.phone_number} FROM ...

```

different exception:  
`org.hibernate.QueryException: No column name found for property [element.phone_number] for alias [pp] [SELECT {p.*}, {pp.element.phone_number} FROM ...`

I’d appreciate any help as I run out of ideas.  
For any help, thanks in advance!

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [May 13, 2025, 2:15pm UTC](https://discourse.hibernate.org/t/elementcollection-with-embeddable-in-native-sql-query/11422/2 "2025-05-13T14:15:39Z")

</div>

@Wojciech_Nowak sounds like you are trying to use native query alias injection with an `@ElementCollection`, and that is not supported by Hibernate ORM. I would kindly ask you to try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a new improvement request in our [issue tracker](https://hibernate.atlassian.net/browse/HHH) and attach that reproducer.

Since you’ve already looked at the code, and possibly identified what’s missing to support this, that would be a great opportunity to make a direct [contribution](https://hibernate.org/community/contribute/) to Hibernate ORM. Let us know if we can help you achieve that.
