How to create hierarchy in OpenMRS Location

From UI I can set the Parent Location while adding a new location. How do I do the same from the Rest API ? My root UUID is 8d6c993e-c2cc-11de-8d13-0010c6dffd0f. I am using a JSON like

$ cat data
{
        "name":"District 3",
        "description":"District 3",
        "parentLocation" : {
            "uuid": "8d6c993e-c2cc-11de-8d13-0010c6dffd0f"
        }
}

Then I send a POST to

curl -H "Content-Type:application/json" -u admin:OpenMRS -X POST http://a.b.c.d:8080/openmrs/ws/rest/v1/location -d @data

Then I get exception

{
  "error": {
    "message": "object references an unsaved transient instance - save the transient instance before flushing: org.openmrs.Location; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.openmrs.Location",
    "code": "org.springframework.orm.hibernate3.SessionFactoryUtils:673",
    "detail": "org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: org.openmrs.Location; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.openmrs.Location\n\tat org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:673)\n\tat org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:794)\n\tat org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:665)\n
  }
}

Please advice what I am doing wrong.

Try changing from this:

"parentLocation" : {
  "uuid": "8d6c993e-c2cc-11de-8d13-0010c6dffd0f"
}

to this:

"parentLocation" : "8d6c993e-c2cc-11de-8d13-0010c6dffd0f"

(The underlying REST API doesn’t properly support letting you post back objects instead of just UUIDs. In some cases you can do this, but in many cases it doesn’t work.)

1 Like

Thanks you. Worked like a charm.

1 Like