When converting the result of a native query to an entity, Hibernate OGM does not check that all the fields are present, this lead to an Entity that does not contain all the values.
Here a testcase to add in the class `MongoDBSessionCLIQueryTest`:
{code} @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(); } } {code}
This is a problem because the value get cashed 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. |
|