0

I am using hibernate ogm 5.1 in my application and I construct this query. This query

    String query = "db.SWMessages.aggregate( [ {0}, {1} ] )";
    Document q1 = new Document("$match",new Document("messageUuid",new 
    Document("$ne" , id.toString())));
    Document q2 = new Document("$group", new Document("_id", 
   "$localReference").append("max", new Document("$max","$creationDate")));
    Object[] param = { q1.toJson(), q2.toJson() };
    String nativeQuery = MessageFormat.format(query, param);
    List<SWMessageR> records = (List<SWMessageR>) em.createNativeQuery(nativeQuery, SWMessageImpl.class)
            .getResultList();

the above code produces the query like this:

  db.SWMessages.aggregate([ { "$match" : { "messageUuid" : { "$ne" : "9c1464d7-311d-4b50-8b81-005bad055232" } } } , { "$group" : { "_id" : "$localReference", "max" : { "$max" : "$creationDate" } } } ])

My question is that the result of this query would return back an entity object which is the SWMessageR?

IsidIoan
  • 403
  • 2
  • 5
  • 13

2 Answers2

0

Well, yes the way you execute your query it will return a List Object of your class. In this way you don't need to use casting, so you can use it like this:

List<SWMessageR> records = em.createNativeQuery(nativeQuery, SWMessageImpl.class).getResultList();

Though, in case you are expecting a single result, I would suggest to use this way:

SWMessageR record = (SWMessageR) em.createNativeQuery( query1, SWMessageR.class ).getSingleResult();

Have a look at the Hibernate Search Documentation Query Section and everything will be clear. Cheers.

Panos
  • 11
  • 5
  • Hi thanks for your answer. However the above query could bring multiple entries so the way you suggest is not helpful. The problem was that I could a persistence exception that hibernate could not map the query results into the entity objects since the group query transforms the returned document, I would suggest you to have a look at mongo documentation. Cheers – IsidIoan Dec 21 '17 at 17:54
0

Hibernate OGM can convert the result into an entity: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#ogm-mongodb-queries-native

I'm not sure what your query returns, but if it's a document in the collection that represents your entity, I expect it to work.

You can see an example in our code base: https://github.com/hibernate/hibernate-ogm/blob/master/mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java#L111

Make sure to pass the expected Entity type, it will look something like this:

    List<SWMessageR> results = em
        .createNativeQuery( nativeQuery, SWMessageR.class )
        .getResultList();

Assuming that SWMessageR is the entity class.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Hi Davide thanks for the reply. The example in github makes a match and sort just the results. The return object is actually the entity. In mongo when you group by the document is transformed and not all the fields are projected. In my case the result is several documents of the type {_id : localReference , max: maxDate } As far I can Understand this can not be mapped to my SWMessageR proivided that the result has only some values in its properties. If in the query I project all the properties that exist in the entity object, would the quey return an Entity object? – IsidIoan Dec 22 '17 at 12:28
  • I would expect it to return null for the values not returned. But I might be wrong because I haven't checked with a test. If the tests in the class I linked don't match your use case, do you think you could write one additional test in that class showing your situation? That would help us answering your question. – Davide D'Alto Jan 08 '18 at 11:17