# Enabling Hibernate Logging

**URL:** https://talk.openmrs.org/t/enabling-hibernate-logging/19140
**Category:** Development
**Created:** [July 25, 2018, 8:46pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140 "2018-07-25T20:46:31Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 25, 2018, 8:46pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/1 "2018-07-25T20:46:31Z")

</div>

Hi there!

I’m trying to make ends meet on merging Patient and Encounter data, but for some reason am yet to find, I’m getting [this](https://pastebin.com/zaFV8KwQ) exception on calling

```
patientService.save(patient);

```

In order to see what really causes the error, I need to turn on Hibernate logging to see whats really going on. But I don’t know how cause I’m calling this core method from my module. Is this possible?

cc: @ssmusoke, @dkayiwa

---

<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: [July 26, 2018, 6:27am UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/2 "2018-07-26T06:27:19Z")

</div>

Are you able to reproduce this in a unit test?

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 26, 2018, 6:29am UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/3 "2018-07-26T06:29:29Z")

</div>

Wat a cool morning it was, the issue was resolved 😄

---

<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: [July 26, 2018, 6:43am UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/4 "2018-07-26T06:43:05Z")

</div>

These threads are searched by others who get into similar problems. Therefore, it would be kind enough to share what the problem was and how you resolved it.

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 27, 2018, 8:22am UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/5 "2018-07-27T08:22:11Z")

</div>

The issue was that I was trying to save a Patient object with references to rows that hibernate expected to be existing in that particular session when in actual sense they are not

**Solution**

I just set those foreign keys to null 🙂

---

<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: [July 27, 2018, 1:32pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/6 "2018-07-27T13:32:07Z")

</div>

> [@samuel34](#):
>
> The issue was that I was trying to save a Patient object with references to rows that hibernate expected to be existing in that particular session when in actual sense they are not

Existing in the session or database? Out of curiosity, you mean those fields should never be set?

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 27, 2018, 2:01pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/7 "2018-07-27T14:01:16Z")

</div>

No that @wyclif the context of the whole thing was that I was trying to merge Patient data using the [mergepatientdata](https://github.com/samuelmale/openmrs-module-mergepatientdata) module

> [@samuel34](#):
>
> this

the above exception was caused at the Validation layer.

> [@wyclif](#):
>
> session or database?

Sorry in the database. 🙂

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 30, 2018, 9:35am UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/8 "2018-07-30T09:35:33Z")

</div>

To be honest I still need your help here.

**How To get this error**

Like for instance you are merging Patients from serverA to B which a new server with Patients. However, data comes with primary keys and foreign keys set yet we use key Generator class of hibernate. Ok I tried as possible to set all these IDs like `patientId` to null to tell hibernate that this is a new Object. However, there some culprit id values that I could be missing to set to null which make Hibernate think that we are updating it ending up with [StaleStateException](https://pastebin.com/SbS8mjMZ)

**Recommended solution**

Check the Hibernate logs to see which row Hibernate is trying to updated or retrieve but I don’t know how to turn the Hibernate logging

see: [https://stackoverflow.com/a/2743663](https://stackoverflow.com/a/2743663)

CC: @dkayiwa, @wyclif, @ssmusoke

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 30, 2018, 12:22pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/9 "2018-07-30T12:22:27Z")

</div>

cc: @dkayiwa, @ssmusoke

---

<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: [July 31, 2018, 3:47pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/10 "2018-07-31T15:47:46Z")

</div>

Did you finally get around this?

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 31, 2018, 8:20pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/11 "2018-07-31T20:20:33Z")

</div>

Yes @dkayiwa

---

<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: [July 31, 2018, 8:37pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/12 "2018-07-31T20:37:01Z")

</div>

Whenever that happens, it is always good to say so and give a summary of how you did so. I almost wasted time trying to look for how to help you on this, not knowing that you had already solved it.

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 31, 2018, 8:40pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/13 "2018-07-31T20:40:08Z")

</div>

I’m sorry, could be you just missed out this

> [@Turning on Hibernate logging](https://talk.openmrs.org/t/turning-on-hibernate-logging/19206/7):
>
> You probably need a unit test to better debug the issue. Append org.hibernate.SQL:debug,org.hibernate.type:trace to the log.level global property, keep in mind it gets very verbose, might be tricky to locate the actual area in the logs you need to look at, this is why a unit test would be nice to have.

---

<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: [July 31, 2018, 8:47pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/14 "2018-07-31T20:47:27Z")

</div>

That was a different thread. Am looking at some one who comes to this thread to help, without knowing that you resolved this using another thread.

---

<div class="post-metadata">

### Author: ![samuel34](https://talk.openmrs.org/user_avatar/talk.openmrs.org/samuel34/32/17813_2.png) [@samuel34](https://talk.openmrs.org/u/samuel34)
#### Post date: [July 31, 2018, 8:50pm UTC](https://talk.openmrs.org/t/enabling-hibernate-logging/19140/15 "2018-07-31T20:50:54Z")

</div>

Issue context

> [@samuel34](#):
>
> How To get this error
> 
> Like for instance you are merging Patients from serverA to B which a new server with Patients. However, data comes with primary keys and foreign keys set yet we use key Generator class of hibernate. Ok I tried as possible to set all these IDs like patientId to null to tell hibernate that this is a new Object. However, there some culprit id values that I could be missing to set to null which make Hibernate think that we are updating it ending up with StaleStateException
> 
> Recommended solution
> 
> Check the Hibernate logs to see which row Hibernate is trying to updated or retrieve but I don’t know how to turn the Hibernate logging

Solution

> [@Turning on Hibernate logging](https://talk.openmrs.org/t/turning-on-hibernate-logging/19206/7):
>
> You probably need a unit test to better debug the issue. Append org.hibernate.SQL:debug,org.hibernate.type:trace to the log.level global property, keep in mind it gets very verbose, might be tricky to locate the actual area in the logs you need to look at, this is why a unit test would be nice to have.
