0
public synchronized static void  saveOrUpdate(MyMessage msg)
{
    if(msg.getId()==0)
        save(msg);
    else{
        EntityManager em = factory.createEntityManager();
        EntityTransaction tx = null;
        MyMessage temp=null;
        try{
            while(em.find(MyMessage.class, msg.getId())==null){
                System.out.println("-----------Object not found----------");
                Thread.sleep(3000);
            }
        tx=em.getTransaction();
        tx.begin();
        temp = em.merge(msg);
        tx.commit();
        em.close();

        msg.setClientId(temp.getClientId());
        msg.setId(temp.getId());
        msg.setMessage(temp.getMessage());
        msg.setReceiverId(temp.getReceiverId());
        msg.setSenderId(temp.getSenderId());
        msg.setStatus(temp.getStatus());
        System.out.println("Sucessfully saved/updated @ server "+temp);

        }catch(Exception e){System.out.println(e);if(tx!=null) tx.rollback();if(em!=null) em.close();}
    }      
}

public static void  save(MyMessage msg)
{
    EntityManager em = factory.createEntityManager();
    EntityTransaction tx = null;
       try{
       tx=em.getTransaction();
       tx.begin();
       em.persist(msg);
       tx.commit();
       em.close();
      System.out.println("Sucessfully saved @ server "+msg);
        }catch(Exception e){System.out.println(e);if(tx!=null) tx.rollback();if(em!=null) em.close();}
}

When saving a new object with saveOrUpdate method it saves the object but next time when I have modified this object in detached mode and saving it via saveOrUpdate method then it always live in while loop, never comes outside the loop. Kindly help me, I am new in JPA and I am converting my project from Hibernate to JPA.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • Can you print what is the value of `m.getId()` you are seeing just before 'find(..)` method? – Madhusudana Reddy Sunnapu Feb 18 '16 at 10:39
  • m.getId()=2916 as expected. When I repeate the process of finding the element again like. EntityManager em1 = factory.createEntityManager(); System.out.println("try to load id first time "+m.getId()); MyMessage msss= em1.find(MyMessage.class, m.getId()); System.out.println(msss); EntityManager em2 = factory.createEntityManager(); System.out.println("try to load id second time "+m.getId()); MyMessage msss2= em2.find(MyMessage.class, m.getId()); System.out.println(msss2); then second time it gives the object from the database. – roushan kumar Singh Feb 18 '16 at 10:44
  • But first time I am not getting any object as I expect. – roushan kumar Singh Feb 18 '16 at 10:45
  • I wonder why it is able to fetch correctly the second time and not the first time itself. Just wanted to check if the code that you are using is same as the one that you posted or minor changes? – Madhusudana Reddy Sunnapu Feb 18 '16 at 11:46
  • and when you issue the "find", what goes in the log? – Neil Stockton Feb 18 '16 at 13:38
  • No there is no changes in this code. I have write the same code as I have used in my project @MadhusudanaReddySunnapu – roushan kumar Singh Feb 19 '16 at 04:53
  • When I save an entity with another thread and getting that entity by other thread, but I m confirm that entity getter theread runs after the saver thread. But the getter thread returns null. @NeilStockton – roushan kumar Singh Feb 19 '16 at 04:55
  • I asked WHAT GOES IN THE LOG. That is the first place to debug anything – Neil Stockton Feb 19 '16 at 05:17
  • Object to save [id=0, senderId=1] Hibernate: select max(id) from messages Hibernate: insert into messages (senderId ,id) values (?, ? ) Sucessfully saved/updated MyMessage [id=2939, senderId=1] trying to get MyMessage from database with id 2939 Hibernate: select mymessage0_.id as id1_2_0_, mymessage0_.senderId as senderId2_2_0 where mymessage0_.id=? MyMessage with id 2939 null @NeilStockton – roushan kumar Singh Feb 19 '16 at 05:55
  • Please provide your xml configuration file and `MyMessage` entity. – Rentius2407 Feb 19 '16 at 06:00
  • It's too long how can I attach it. – roushan kumar Singh Feb 19 '16 at 06:26
  • If it is possible add it to GitHub and post the link here. – Rentius2407 Feb 19 '16 at 06:51
  • I got the solution. It was the problem of isolation level setting. By default hibernate implements Read_Commited. But when we use Hibernate with JPA it uses Repeated_read. So We have to implicitly write property in persistence.xml file. – roushan kumar Singh Feb 26 '16 at 08:48

1 Answers1

0

1) It's look like that you have problem with Second-Level Cache in JPA. Try to check it (I added new line with comment in your code):

public static MyMessage saveOrUpdate(MyMessage msg) {
    ...
    EntityManager em1 = factory.createEntityManager();
    Cache cache = em1.getEntityManagerFactory().getCache(); // get cache 
    cache.evictAll(); // remove all Entity from Cache
    MyMessage msss= em1.find(MyMessage.class, m.getId());
    System.out.println(msss);// prints null
    return m;
} 

OR

2) Try to force flush using

    em.flush();
    EntityManager em1 = factory.createEntityManager();
    MyMessage msss= em1.find(MyMessage.class, m.getId());
    System.out.println(msss);// prints null
    return m;

3) Try to change merged to persist, instead of m = em.merge(msg); use em.persist(msg); m = msg;