Uploaded image for project: 'Hibernate OGM'
  1. OGM-1375

We shouldn't create partial entities from a native query

    Details

      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`:

      	@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.

        Attachments

          Issue links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Stride room