We're updating the issue view to help you get more done. 

We shouldn't create partial entities from a native query

Description

When converting the result of a native query to an entity, Hibernate OGM does not check that all the fields are present, this leads to an Entity that does not contain all the values.

Here a testcase to add in the class `MongoDBSessionCLIQueryTest`:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Test public void testPartialCreationOfEntity() throws Exception { try ( OgmSession session = openSession() ) { Transaction transaction = session.beginTransaction(); String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({ 'name' : '" + portia.getName() + "' }, {'name': 1, 'rating':1})"; Query query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class ); OscarWildePoem result = (OscarWildePoem) query.uniqueResult(); assertThat( result.getAuthor() ).isEqualTo( portia.getAuthor() ); assertThat( result.getCopiesSold() ).isEqualTo( portia.getCopiesSold() ); transaction.commit(); } }

This is a problem because the value gets cached and can result in unexpected results when running subsequent queries.

I guess we should check what Hibernate ORM does, I suppose an exception should be thrown in this scenario.

See this link to stack overflow: https://stackoverflow.com/questions/48294248/hibernate-ogm-with-mongodb-result-of-2nd-query-on-same-table-dependent-on-result

We should check if the same behavior happens for the other dialects and for HQL queries.

Environment

None

Status

Assignee

Fabio Massimo Ercoli

Reporter

Davide D'Alto

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Priority

Major