1

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?

Porjaz
  • 771
  • 1
  • 8
  • 28

2 Answers2

0

Try changing your query to:

MATCH (p:Person {id:3})
RETURN { firstname: p.firstname, lastname: p.lastname }
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • This works fine. I am getting a result like this `[{firstname=Jon, lastname=Smith}]` . I can convert this array into a proper JSON afterwards. – Porjaz Sep 21 '17 at 13:31
  • @Porjaz Try this trick: `MATCH (p:Person {id:3}) RETURN collect({ firstname: p.firstname, lastname: p.lastname })[0]` – Bruno Peres Sep 21 '17 at 13:35
  • It still gives them in the same format `[{firstname=Jon, lastname=Smith}]` – Porjaz Sep 21 '17 at 13:41
  • @Porjaz This is probably related to the way your Java code is converting the query result. I suggest you to ask a new question about it. – Bruno Peres Sep 21 '17 at 13:43
  • Its not that big of a problem. I can write a code to convert it to a proper json. Thank you very much for the help – Porjaz Sep 21 '17 at 13:45
0

If you are looking for the returned value to be an entity, this will work:

String query = "MATCH (p:Person {id:3}) RETURN p";
List<Person> person = (List<Person>) em.createNativeQuery(query, Person.class).getResultList();

Actually, in this case this would make more sense:

    Person poem = (Person) em.createNativeQuer(query, Person.class).getSingleResult();
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • I think this is not quite what I am looking for. I am trying to return a subgraph (a family tree) so the query is quite big. That's why I need to return every person's firstname, lastname, relationship, relationship_property, etc. Bruno Peres's query returns the data in a close way that I was looking for. It returned `[{firstname=Jon, lastname=Smith}]`. Ideally it would be best if the returned result could be in a json format – Porjaz Sep 21 '17 at 14:44
  • Hibernate OGM currently does not support ResultTransformers, I've created an issue for it: https://hibernate.atlassian.net/browse/OGM-1328 – Davide D'Alto Sep 26 '17 at 12:08