# Saving DrugOrder fails with org.hibernate.LazyInitializationException: could not initialize proxy - no Session

**URL:** https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074
**Category:** Development
**Created:** [July 1, 2016, 8:03pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074 "2016-07-01T20:03:17Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [July 1, 2016, 8:03pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/1 "2016-07-01T20:03:17Z")

</div>

Hello, I am currently working on an Order Entry module involving drug orders. When I try to save my order, it fails

WIth the following code DrugOrder dorder = new DrugOrder(); dorder.setRoute(Context.getConceptService().getConceptByName(“Oral”)); dorder = (DrugOrder) Context.getOrderService().saveOrder(dorder, null);

I get the error The route concept must be among allowed concepts as specified by the order.drugRoutesConceptUuid global property

orderService.getDrugRoutes() does not return anything

The same applies for DoseUnits, Duration Units, QuantityUnits, Frequency etc.

Could anyone please guide me…

At present I have a model drugorders where I am able to save where I am able to save the selected Route options (Drop-down) int routeConcept = Context.getConceptService().getConceptByName(drugRoute).getConceptId(); drugorders.setRoute(routeConcept); But I am unable to save this as an Order of type Drug Order

> **[HariniParth/OpenMRS-DrugOrders](https://github.com/HariniParth/OpenMRS-DrugOrders)**
>
> Contribute to HariniParth/OpenMRS-DrugOrders development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![darius](https://talk.openmrs.org/user_avatar/talk.openmrs.org/darius/32/8082_2.png) [@darius](https://talk.openmrs.org/u/darius)
#### Post date: [July 2, 2016, 5:57pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/2 "2016-07-02T17:57:49Z")

</div>

There is a global property that you need to set, and it needs to point to a concept that defines a set of route options.

-Darius

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [July 3, 2016, 4:57am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/3 "2016-07-03T04:57:32Z")

</div>

Thank you! Got it to save.

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [July 24, 2016, 11:14pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/4 "2016-07-24T23:14:34Z")

</div>

I have been successful in creating a Drug Order form which saves the order. Now I have introduced a ‘Confirm Order’ fragment which allows the user to review the order prior to submitting it. i.e. I have two forms - one where I enter all the order specific details - drug name, start date, dose, duration etc. which on submission takes me to another form (fragment) where I can review the Order However, I am getting the following error - org.hibernate.LazyInitializationException: could not initialize proxy - no Session

My code is something like this - The first form has the following action -

if(“addOrderDraft”.equals(action)){ createNewDrugOrder(DrugOrder order){ order = new DrugOrder; //Setting all the parameters - dose, duration, frequency, provider, encounter etc //Adding the DrugOrder s to an arrayList of the type DrugOrder arrayList.add(order); } }

The second form tries to save the Order -

if(“confirmOrder”.equals(action)){ for(DrugOrder dO : arrayList) { dO = (DrugOrder)Context.getOrderService().saveOrder(dO, null); } }

Earlier, I was calling the saveOrder function inside the first form posted above and it worked! Could anyone please guide me on how to fix this.

---

<div class="post-metadata">

### Author: ![lluismf](https://talk.openmrs.org/user_avatar/talk.openmrs.org/lluismf/32/770_2.png) [@lluismf](https://talk.openmrs.org/u/lluismf)
#### Post date: [July 25, 2016, 9:01pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/5 "2016-07-25T21:01:26Z")

</div>

When the second form is processed the beans in arrayList are dettached, so accessing any attribute not loaded brings up the lazy initialization exception. To fix it, just read the attribute in the first form.

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [July 26, 2016, 5:01pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/6 "2016-07-26T17:01:31Z")

</div>

Thank you…Could you please give me a code example…somehow not able to get it working…

I have my ArrayList of the type DrugOrder defined in a separate class class AL{ public static List drugOrderMain = new ArrayList();

When the first form is submitted, all the setters for DrugOrder (dose, route etc.) are done and the object is added to the ArrayList

AL.drugOrderMain.add(order);

Thereafter in the next submit action, I try to save it…

List drugOrderMain = AL.getDrugOrderMain(); for(DrugOrder dorderMain : drugOrderMain){ dorderMain = (DrugOrder)Context.getOrderService().saveOrder(dorderMain, null);

The idea is to have a group of Orders saved together…

---

<div class="post-metadata">

### Author: ![lluismf](https://talk.openmrs.org/user_avatar/talk.openmrs.org/lluismf/32/770_2.png) [@lluismf](https://talk.openmrs.org/u/lluismf)
#### Post date: [July 26, 2016, 6:43pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/7 "2016-07-26T18:43:57Z")

</div>

Check the exception stack trace and see which getter is throwing it. Then invoke the getter in the first controller. How are you passing this list of drug orders between fragments ? Session, request ?

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [July 29, 2016, 4:51am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/8 "2016-07-29T04:51:15Z")

</div>

The line which throws an error dorderMain = (DrugOrder)Context.getOrderService().saveOrder(dorderMain, null);

I define an action input for each form (

1. Setting the parameters of the order
2. Saving the Order) In my Page Controller I check what the action string is equal to and then call the set/save functions…

if(“addOrderDraft”.equals(action)){ DrugOrder do = new DrugOrder; do.setDose do.setDuration etc…etc… }

if(“confirmOrder”.equals(action)){ Here I am able to retrieve and view the parameters of the drugorder but unable to save! drugorder = (DrugOrder)Context.getOrderService().saveOrder(drugorder, null); }

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [August 3, 2016, 8:23pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/9 "2016-08-03T20:23:29Z")

</div>

Hello, Could anyone please guide me with how to save and update the the DrugOrder object in the session. I have not been able to find the way to implement this. I have the DrugOrder fields (route, dose, duration, etc.) being set and added to a HashMap in one form and I am saving them in the DB in a different form.

When I tried giving session = sessionFactory.getCurrentSession(); session.save(object), it fails with a null pointer exception

Here is my DrugOrderPageController

> <https://github.com/HariniParth/OpenMRS-DrugOrders/blob/master/omod/src/main/java/org/openmrs/module/drugorders/page/controller/DrugordersPageController.java>

Please help.Thanks

---

<div class="post-metadata">

### Author: ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)
#### Post date: [August 4, 2016, 3:43pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/10 "2016-08-04T15:43:44Z")

</div>

If you’re getting a lazy initialization exception, it means the session is closed which happens when there is an exception thrown, so you might want to look closely at the error logs and fix why an exception is getting thrown

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [August 5, 2016, 4:38am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/11 "2016-08-05T04:38:00Z")

</div>

In my second form where I confirm and save the order is where I get a null pointer error

DrugOrder order = confirmedListMain.get(i); order = (DrugOrder) Context.getOrderService().saveOrder(order, null);

I have tried following the suggestions I found online (using hibernate.initialize, session.update, session.merge ) but haven’t been able to successfully fix the issue. Could you please take a look at my code. It is posted in my previous post. Please give me a example code which I can follow…

---

<div class="post-metadata">

### Author: ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)
#### Post date: [August 5, 2016, 7:35am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/12 "2016-08-05T07:35:09Z")

</div>

@hariniparth are you able to reproduce this in a unit test?

---

<div class="post-metadata">

### Author: ![lluismf](https://talk.openmrs.org/user_avatar/talk.openmrs.org/lluismf/32/770_2.png) [@lluismf](https://talk.openmrs.org/u/lluismf)
#### Post date: [August 5, 2016, 8:06am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/13 "2016-08-05T08:06:12Z")

</div>

> [@hariniparth](#):
>
> In my second form where I confirm and save the order is where I get a null pointer error

A stack trace of the null pointer would help 🙂But it’s a different problem than the lazy initialization exception.

---

<div class="post-metadata">

### Author: ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)
#### Post date: [August 5, 2016, 6:03pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/14 "2016-08-05T18:03:02Z")

</div>

Do you have a unit test where you can reproduce the issue? [Here](https://github.com/openmrs/openmrs-core/blob/master/api/src/test/java/org/openmrs/OrderEntryIntegrationTest.java#L90) is a test that shows how you create a drug order

---

<div class="post-metadata">

### Author: ![lluismf](https://talk.openmrs.org/user_avatar/talk.openmrs.org/lluismf/32/770_2.png) [@lluismf](https://talk.openmrs.org/u/lluismf)
#### Post date: [August 5, 2016, 7:24pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/15 "2016-08-05T19:24:47Z")

</div>

> [@hariniparth](#):
>
> Here is my DrugOrderPageController

I’m not a Spring master by any means, but the controller method with ~50 parameters is a code smell.

Also, don’t use static variables in a controller 😧

> public class ConfirmOrderFragmentController {
> 
> ```
> public static int currentDraftOrderIndex;
> public static HashMap<Integer,DrugOrder> drugOrderMain = new HashMap<Integer,DrugOrder> ();
> public static HashMap<Integer,drugorders> drugOrderExtension = new HashMap<Integer,drugorders>();
> 
> ```

Check this answer:

> <https://stackoverflow.com/questions/11139571/scope-of-a-spring-controller-and-its-instance-variables>

I think it would be easy with just 1 controller instead of 2, but most important is to keep the drug order attributes in request or session, not in the controller. And if you keep instances of DrugOrder make sure that all its attributes are normal objects, not Hibernate proxies.

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [August 7, 2016, 1:45am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/16 "2016-08-07T01:45:58Z")

</div>

Thank you, I will follow your suggestions. But could you please help me with the code to open the session and save / update the object…I have attached the file containing the error log.

This is where it fails - order = (DrugOrder) Context.getOrderService().saveOrder(order, null);

However, I am able to save the drug orders when i add this line in the method where is set the parameters…

[lazyHibernate.txt](https://talk.openmrs.org/uploads/default/original/2X/7/733c3bacb8a46003f39af0a29167cb826868f92e.txt) (10.8 KB)

---

<div class="post-metadata">

### Author: ![lluismf](https://talk.openmrs.org/user_avatar/talk.openmrs.org/lluismf/32/770_2.png) [@lluismf](https://talk.openmrs.org/u/lluismf)
#### Post date: [August 7, 2016, 9:22pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/17 "2016-08-07T21:22:57Z")

</div>

Session is opened by a web filter, you don’t need to do anything. The problem is that you are keeping a Hibernate proxy (with non initialized attributes) in a static variable. The second controller creates a new session (in a new request) that’s why you are getting this:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) at org.openmrs.Person\_$$_jvst867\_2a.getNames(Person_$$\_jvst867\_2a.java) at org.openmrs.Provider.toString(Provider.java:107) at org.springframework.validation.ValidationUtils.rejectIfEmpty(ValidationUtils.java:166) at org.springframework.validation.ValidationUtils.rejectIfEmpty(ValidationUtils.java:108) at org.openmrs.validator.OrderValidator.validate(OrderValidator.java:83) at org.openmrs.validator.DrugOrderValidator.validate(DrugOrderValidator.java:83)

---

<div class="post-metadata">

### Author: ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)
#### Post date: [August 8, 2016, 4:38pm UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/18 "2016-08-08T16:38:15Z")

</div>

Have you gotten rid of the static variables in controllers as @lluismf suggested?

---

<div class="post-metadata">

### Author: ![hariniparth](https://talk.openmrs.org/letter_avatar/hariniparth/32/5_5575768a8748004e209b776fc1b2916d.png) [@hariniparth](https://talk.openmrs.org/u/hariniparth)
#### Post date: [August 12, 2016, 3:39am UTC](https://talk.openmrs.org/t/saving-drugorder-fails-with-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session/7074/19 "2016-08-12T03:39:35Z")

</div>

Hello, Thanks everyone for the help and sorry for the delayed response…Yes I am not using the static variables now. Still trying to fix.
