Question

Seriously, i cannot believe this:

I use entityManager in Vaadin framework. Implemented this pattern (ThreadLocal and filter for generating entityManager for every request):

https://vaadin.com/book/-/page/jpacontainer.hibernate.html

If i write a test it is works like charm:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("applicLiquidator.applicLiquidator");
log.trace("created entityManageFactory:" + entityManagerFactory);
EntityManager entityManager = entityManagerFactory.createEntityManager();

CaseEntity case= entityManager.getReference(CaseEntity .class, 1);
entityManager.detach(case);
case.setCompanyFullName("TEST1");

entityManager.getTransaction().begin();
CaseEntity newCase=entityManager.merge(case);
entityManager.getTransaction().commit();

However in the application server it works entirely other way, i logged ever information and seems everything is OK, the entity has the good values!

   public static int saveCase(CaseEntity case, boolean sameTransaction) throws DALException {
        EntityTransaction transaction = null;
        try {
            EntityManager entityManager = EntityManagerUtil.getEntityManagerLIQUIDATOR();
            if (sameTransaction == false) {
                transaction = entityManager.getTransaction();
                transaction.begin();
            }
                      log.info("-------BEFORE PERSIST: CASE ENTTIY COMAPNY NAME: " + ugy.getFelszamolandoCegTeljesnev());
        ugy=entityManager.merge(ugy);
        if (sameTransaction == false) {
            transaction.commit();
        }

        log.info("---------AFTER PERSIST: CASE ENTTIY COMAPNY NAME: " + ugy.getFelszamolandoCegTeljesnev());  
            return case.getId();
        } catch (Exception ex) {
            log.error("The entity saved do not completed", ex);
            try {
                transaction.rollback();
                log.info("Rollback was success");
            } catch (Exception rollbex) {
                log.error("Rollback failed", rollbex);
            }
            log.info("Entity Saved succesfully!");
            throw new DALException(ex);
        }
    }

As you can see, i tried refresh, clear, flush everything. Important to note here that case is a detached object, but as far as i know merge will do the job but the reference will not copied to the original object, but will (should) update the entity with the same id...

In other cases (with other entites this code is works like charm), only with one case, the first one entity at the database with id 1 is making bug!

I cannot understand why it is, why it is working some cases, and why it is not... And why it is that i see the good values in the entity attibutes and it just doesn't generate the update sql and doesn't make the merge/update in the persistance context and also at the database, no error message, nothing to show, only missfunction...

If anyone ever experience this kind of mistic bug/issue i would more than appreciate the answer or any slight idea, anything came in to your mind pls take time to answer.

Log when try to merge entity:

2014-01-21 17:36:11 TRACE LazyEntityManagerFilter:44 - created entityManager:org.eclipse.persistence.internal.jpa.EntityManagerImpl@7b1fe9fb
2014-01-21 17:36:11 WARN  UgyController:667 - Event detected private void notImplemented()
2014-01-21 17:36:11 TRACE LazyEntityManagerFilter:51 - destroyed entityManager:org.eclipse.persistence.internal.jpa.EntityManagerImpl@7b1fe9fb
2014-01-21 17:36:11 TRACE LazyEntityManagerFilter:44 - created entityManager:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.387--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, becsultertek, bekerulesidopontja, devizanem, ingosagazonosito, kikerulesidopontja, kikerulesiertek, letrehozva, megnevezes, memo, tarolasihely, telephelyenbelulihelye, tranzakcioskod, utoljaramod_mikor, telephely_felszamolando_ID, ingosagcsoport_ID, utoljaramodositotta_felhasznalo_ID FROM liquidator.ingosag WHERE (telephely_felszamolando_ID = ?)
    bind => [1]
[EL Fine]: 2014-01-21 17:36:11.389--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, csoportnev, letrehozva, lockideje, tranzakcioskod, utoljaramod_mikor, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ingosagcsoport WHERE (ID = ?)
    bind => [1]
[EL Fine]: 2014-01-21 17:36:11.392--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, csoportnev, letrehozva, lockideje, tranzakcioskod, utoljaramod_mikor, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ingosagcsoport WHERE (ID = ?)
    bind => [2]
[EL Fine]: 2014-01-21 17:36:11.394--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, bejelentkezesidopontja, bejelentkezesinev, email, jelszo, keresztnev, kilepesideje, letrehozva, lockideje, memo, sessionlejarat, szerepkor, telefon, tranzakcioskod, utoljaramod_mikor, vezeteknev, cim_ID, utoljaramodositotta_felhasznalo_ID, bejelentkezes_telephelyId, lockbyfelhasznalo_ID FROM liquidator.felhasznalo WHERE (ID = ?)
    bind => [3]
[EL Fine]: 2014-01-21 17:36:11.398--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, fajlnev, kiskeptartalom, letrehozva, memo, mimetype, nincskep, tartalom, tranzakcioskod, utoljaramod_mikor, ingosag_ID, utoljaramodositotta_felhasznalo_ID FROM liquidator.kep WHERE (ingosag_ID = ?)
    bind => [1]
[EL Fine]: 2014-01-21 17:36:11.4--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, fajlnev, kiskeptartalom, letrehozva, memo, mimetype, nincskep, tartalom, tranzakcioskod, utoljaramod_mikor, ingosag_ID, utoljaramodositotta_felhasznalo_ID FROM liquidator.kep WHERE (ingosag_ID = ?)
    bind => [2]
[EL Fine]: 2014-01-21 17:36:11.401--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, becsultertek, bekerulesidopontja, devizanem, ingosagazonosito, kikerulesidopontja, kikerulesiertek, letrehozva, megnevezes, memo, tarolasihely, telephelyenbelulihelye, tranzakcioskod, utoljaramod_mikor, telephely_felszamolando_ID, ingosagcsoport_ID, utoljaramodositotta_felhasznalo_ID FROM liquidator.ingosag WHERE (telephely_felszamolando_ID = ?)
    bind => [2]
[EL Fine]: 2014-01-21 17:36:11.402--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, becsultertek, bekerulesidopontja, devizanem, ingosagazonosito, kikerulesidopontja, kikerulesiertek, letrehozva, megnevezes, memo, tarolasihely, telephelyenbelulihelye, tranzakcioskod, utoljaramod_mikor, telephely_felszamolando_ID, ingosagcsoport_ID, utoljaramodositotta_felhasznalo_ID FROM liquidator.ingosag WHERE (telephely_felszamolando_ID = ?)
    bind => [3]
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.404--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, belsougyiratszam, elrendeles_doc_fajlnev, elrendeles_doc_memo, elrendeles_doc_mimetype, elrendeles_doc_tartalom, elrendeles_idopontja, elrendeles_ki, elrendeles_memo, felszamolando_ceg_adoszam, felszamolando_ceg_cegjegyzekszam, felszamolando_ceg_memo, felszamolando_ceg_rovidnev, felszamolando_ceg_teljesnev, kozzeteteldatuma, kulsougyiratszam, letrehozva, lockideje, memo, tranzakcioskod, utoljaramod_mikor, zarovegzesdatuma, elrendeles_felhasznalo_ID, ugy_felhasznalo_ID, felszamolando_ceg_cim_ID, telephely_ID, statusz_ID, ugytipus_ID, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ugy WHERE ((ID = ?) AND (aktiv = ?))
    bind => [7, true]
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.41--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, letrehozva, lockideje, statusz, tranzakcioskod, utoljaramod_mikor, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ugystatusz WHERE (aktiv = ?)
    bind => [true]
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.413--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT DISTINCT f.* FROM felhasznalo f,telephely, felhasznalougyintezo_telephely ftu WHERE f.aktiv = 1 AND telephely.ID = ? AND telephely.aktiv = 1 AND f.ID = ftu.felhasznalo_ID AND ftu.telephely_ID = ?
    bind => [1, 1]
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.418--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, letrehozva, lockideje, tipus, tranzakcioskod, utoljaramod_mikor, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ugytipus WHERE (aktiv = ?)
    bind => [true]
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.421--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT DISTINCT f.* FROM felhasznalo f,telephely, felhasznalougyintezo_telephely ftu WHERE f.aktiv = 1 AND telephely.ID = ? AND telephely.aktiv = 1 AND f.ID = ftu.felhasznalo_ID AND ftu.telephely_ID = ?
    bind => [1, 1]
telephelyTelephely A
telephelyTelephely B
telephelyTelephely (törölt)
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.449--ClientSession(1062469667)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--UPDATE liquidator.felhasznalo SET bejelentkezes_telephelyId = ? WHERE (ID = ?)
    bind => [1, 9999999]
[EL Fine]: 2014-01-21 17:36:11.45--ClientSession(1062469667)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--UPDATE liquidator.telephely_felszamolando SET tranzakcioskod = ?, utoljaramod_mikor = ?, utoljaramodositotta_felhasznalo_ID = ? WHERE (ID = ?)
    bind => [8d7b4195-38f3-4ba6-ab13-2840de6291d4, 2014-01-21 17:36:11.384, 9999999, 1]
[EL Fine]: 2014-01-21 17:36:11.452--ClientSession(1062469667)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--UPDATE liquidator.telephely_felszamolando SET tranzakcioskod = ?, utoljaramod_mikor = ?, utoljaramodositotta_felhasznalo_ID = ? WHERE (ID = ?)
    bind => [8d7b4195-38f3-4ba6-ab13-2840de6291d4, 2014-01-21 17:36:11.384, 9999999, 2]
[EL Fine]: 2014-01-21 17:36:11.453--ClientSession(1062469667)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--UPDATE liquidator.telephely_felszamolando SET tranzakcioskod = ?, utoljaramod_mikor = ?, utoljaramodositotta_felhasznalo_ID = ? WHERE (ID = ?)
    bind => [8d7b4195-38f3-4ba6-ab13-2840de6291d4, 2014-01-21 17:36:11.384, 9999999, 3]
[EL Fine]: 2014-01-21 17:36:11.467--ClientSession(1062469667)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--UPDATE liquidator.ingosag SET tranzakcioskod = ?, utoljaramod_mikor = ?, utoljaramodositotta_felhasznalo_ID = ? WHERE (ID = ?)
    bind => [8d7b4195-38f3-4ba6-ab13-2840de6291d4, 2014-01-21 17:36:11.384, 9999999, 1]
[EL Fine]: 2014-01-21 17:36:11.468--ClientSession(1062469667)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--UPDATE liquidator.ingosag SET tranzakcioskod = ?, utoljaramod_mikor = ?, utoljaramodositotta_felhasznalo_ID = ? WHERE (ID = ?)
    bind => [8d7b4195-38f3-4ba6-ab13-2840de6291d4, 2014-01-21 17:36:11.384, 9999999, 2]
2014-01-21 17:36:11 INFO  UgyController$UgyPersistor:978 - UgyEntity módosítása a DB-ben sikeres. UgyEntityID: 7
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.524--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, belsougyiratszam, elrendeles_doc_fajlnev, elrendeles_doc_memo, elrendeles_doc_mimetype, elrendeles_doc_tartalom, elrendeles_idopontja, elrendeles_ki, elrendeles_memo, felszamolando_ceg_adoszam, felszamolando_ceg_cegjegyzekszam, felszamolando_ceg_memo, felszamolando_ceg_rovidnev, felszamolando_ceg_teljesnev, kozzeteteldatuma, kulsougyiratszam, letrehozva, lockideje, memo, tranzakcioskod, utoljaramod_mikor, zarovegzesdatuma, elrendeles_felhasznalo_ID, ugy_felhasznalo_ID, felszamolando_ceg_cim_ID, telephely_ID, statusz_ID, ugytipus_ID, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ugy WHERE ((ID = ?) AND (aktiv = ?))
    bind => [7, true]
2014-01-21 17:36:11 INFO  UgyController$UgyPersistor:998 - Ügy egészének perzisztálása sikeres. UgyEntityID: 7
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.528--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, belsougyiratszam, elrendeles_doc_fajlnev, elrendeles_doc_memo, elrendeles_doc_mimetype, elrendeles_doc_tartalom, elrendeles_idopontja, elrendeles_ki, elrendeles_memo, felszamolando_ceg_adoszam, felszamolando_ceg_cegjegyzekszam, felszamolando_ceg_memo, felszamolando_ceg_rovidnev, felszamolando_ceg_teljesnev, kozzeteteldatuma, kulsougyiratszam, letrehozva, lockideje, memo, tranzakcioskod, utoljaramod_mikor, zarovegzesdatuma, elrendeles_felhasznalo_ID, ugy_felhasznalo_ID, felszamolando_ceg_cim_ID, telephely_ID, statusz_ID, ugytipus_ID, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ugy WHERE (aktiv = ?)
    bind => [true]
2014-01-21 17:36:11 INFO  EntityManagerUtil:46 - entityManager object name@hascode:org.eclipse.persistence.internal.jpa.EntityManagerImpl@6c3a9ce3
[EL Fine]: 2014-01-21 17:36:11.54--ServerSession(2071159129)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-19,5,main])--SELECT ID, aktiv, belsougyiratszam, elrendeles_doc_fajlnev, elrendeles_doc_memo, elrendeles_doc_mimetype, elrendeles_doc_tartalom, elrendeles_idopontja, elrendeles_ki, elrendeles_memo, felszamolando_ceg_adoszam, felszamolando_ceg_cegjegyzekszam, felszamolando_ceg_memo, felszamolando_ceg_rovidnev, felszamolando_ceg_teljesnev, kozzeteteldatuma, kulsougyiratszam, letrehozva, lockideje, memo, tranzakcioskod, utoljaramod_mikor, zarovegzesdatuma, elrendeles_felhasznalo_ID, ugy_felhasznalo_ID, felszamolando_ceg_cim_ID, telephely_ID, statusz_ID, ugytipus_ID, utoljaramodositotta_felhasznalo_ID, lockbyfelhasznalo_ID FROM liquidator.ugy WHERE ((ID = ?) AND (aktiv = ?))

bind => [7, true]

But when i want to save/merge another entity (e.g. ID 1303), the insert is nicely generated and executed:

  [EL Fine]: 2014-01-21 17:38:19.391--ClientSession(1093415948)--Connection(264299847)--Thread(Thread[http-bio-8080-exec-22,5,main])--UPDATE liquidator.ugy SET felszamolando_ceg_teljesnev = ?, lockideje = ?, tranzakcioskod = ?, utoljaramod_mikor = ?, lockbyfelhasznalo_ID = ? WHERE (ID = ?)
        bind => [akgalkgsafafsTESTESTUJUJ, null, 52254092-4954-441f-922c-8c5527b9dd9a, 2014-01-21 17:38:19.368, null, 1303]

LOG UPDATE WITH NEW CODE

2014-01-21 17:55:19 INFO  UgyDAO:162 - -------BEFORE PERSIST: CASE ENTTIY COMAPNY NAME: NEW COMPANY NAME
[EL Fine]: 2014-01-21 17:55:19.19--ClientSession(1760590618)--Connection(392035700)--Thread(Thread[http-bio-8080-exec-46,5,main])--UPDATE liquidator.telephely_felszamolando SET tranzakcioskod = ?, utoljaramod_mikor = ? WHERE (ID = ?)
    bind => [fe48707b-5064-49c5-81bf-d1f78cd93126, 2014-01-21 17:55:19.156, 1]
[EL Fine]: 2014-01-21 17:55:19.194--ClientSession(1760590618)--Connection(392035700)--Thread(Thread[http-bio-8080-exec-46,5,main])--UPDATE liquidator.telephely_felszamolando SET tranzakcioskod = ?, utoljaramod_mikor = ? WHERE (ID = ?)
    bind => [fe48707b-5064-49c5-81bf-d1f78cd93126, 2014-01-21 17:55:19.156, 2]
[EL Fine]: 2014-01-21 17:55:19.197--ClientSession(1760590618)--Connection(392035700)--Thread(Thread[http-bio-8080-exec-46,5,main])--UPDATE liquidator.telephely_felszamolando SET tranzakcioskod = ?, utoljaramod_mikor = ? WHERE (ID = ?)
    bind => [fe48707b-5064-49c5-81bf-d1f78cd93126, 2014-01-21 17:55:19.156, 3]
[EL Fine]: 2014-01-21 17:55:19.198--ClientSession(1760590618)--Connection(392035700)--Thread(Thread[http-bio-8080-exec-46,5,main])--UPDATE liquidator.ingosag SET tranzakcioskod = ?, utoljaramod_mikor = ? WHERE (ID = ?)
    bind => [fe48707b-5064-49c5-81bf-d1f78cd93126, 2014-01-21 17:55:19.156, 1]
[EL Fine]: 2014-01-21 17:55:19.201--ClientSession(1760590618)--Connection(392035700)--Thread(Thread[http-bio-8080-exec-46,5,main])--UPDATE liquidator.ingosag SET tranzakcioskod = ?, utoljaramod_mikor = ? WHERE (ID = ?)
    bind => [fe48707b-5064-49c5-81bf-d1f78cd93126, 2014-01-21 17:55:19.156, 2]
2014-01-21 17:55:19 INFO  UgyDAO:168 - ---------AFTER PERSIST: CASE ENTTIY COMAPNY NAME: UJ TELJES NEV!
Was it helpful?

Solution

I very believe however yet no time to POC/test it, but my problem was caused because the poorly implemented hashcode() and equals() methods!

As it seems reference this article: http://tmjee.blogspot.hu/2012/11/do-we-need-equals-and-hashcode.html

That poor hashcode implementation can cause merge problems in detached objects, and that is exactly my issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top