javahibernateneo4jhibernate-ogmhibernate-native-query

Hibernate Neo4j retrieve result in a key-value form


I would like to retrieve entity properties in a format like this: property_name: value.

I am trying to get the result this way:

public void retrievePerson(){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
      try{

         String query = "MATCH (p:Person {id:3}) RETURN p.firstname, p.lastname";

         List<Object[]> person = (List<Object[]>) em.createNativeQuery(query).getResultList();

         em.flush();
         tx.commit();
         em.clear();
         em.close();
         emf.close();

      }
      catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }
   }

I read somewhere the the object returned by the query is a managed entity.

I would like the result to be like this: {"firstname":"Jon", "lastname":"Smith"}

I have found this setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE which might be something that I am looking for but I am unable to get it to work.

Is there a way to achieve this?


Solution

  • Try changing your query to:

    MATCH (p:Person {id:3})
    RETURN { firstname: p.firstname, lastname: p.lastname }