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

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

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.


Thank you! Got it to save.

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.

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.

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


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…

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 ?

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); }

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();, it fails with a null pointer exception

Here is my DrugOrderPageController

Please help.Thanks

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

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…

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

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

Do you have a unit test where you can reproduce the issue? Here is a test that shows how you create a drug order

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 :anguished:

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:

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.

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 (10.8 KB)

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( at org.hibernate.proxy.AbstractLazyInitializer.getImplementation( at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke( at org.openmrs.Person_$$jvst867_2a.getNames(Person$$ at org.openmrs.Provider.toString( at org.springframework.validation.ValidationUtils.rejectIfEmpty( at org.springframework.validation.ValidationUtils.rejectIfEmpty( at org.openmrs.validator.OrderValidator.validate( at org.openmrs.validator.DrugOrderValidator.validate(

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

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.