Here is the code snippet which aims to accomplish the task of whether a provider with a given person exists. This is inside the showForm() method of UserFormController in Legacyui.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.openmrs.Person
The complete stack trace is here:
I had tried adding a Casacade Annotation as well but it seemed to fail (Maybe I added it at the wrong location?). Any help for this issue is appreciated. Also, why does this happen exactly? I have tried running the same code in the handleSubmission method and it works fine.
Can you give more details of when this happens because i.e does happen when youâre creating a new user or editing an existing user? And are you creating the user from an existing person or are you creating the person record too on the fly? You normally get this kind of exception when a persistent object is flushed to the DB with one of its associations is new (unknown to hibernate) and the flush can happen when you call any transactional method within the same session in your case youâre calling ProviderService.getProvidersByPerson() which cases the flush
I still need to know under what scenarios you run into this or does it happen all the time regardless? Because i bet you wouldnât run into where the person record already exists or if youâre creating a new user account
That info now helps, the problem is that in showForm() method there is 2 lines where youâre calling ProviderService.getProvidersByPerson(Person) when adding a new user account, the person object is new so the personId is null, this causes hibernate to complain since it requires the personId to join to the provider table. And also if you think about logically you donât expect a non existent person to have a provider account, the ensuing if clause in your PR actually does the correct thing by first checking if itâs an existing person account before calling getProvidersByPerson
Youâre already doing the correct check in the if clause right after those 2 calls to getProvidersByPerson(), just remove those 2 lines and it will be fine