0

What is the difference between entitymanager.merge and query.executeUpdate.

I have one entity order

@Entity
@Table(name="ORDERS",schema ="AMZ")
public class Orders {

    @Id
    @SequenceGenerator(name = "pk_sequence", sequenceName = "ORDERS_SEQ", schema = "AMZ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_sequence")
    @Column(name = "ORDERS_ID")
    private long id;

    @Column(name = "STATUS")
    private Status status;

I have my DAO class

@Repository
@Transactional
public class InvoiceDAO {

    @PersistenceContext
    private EntityManager entityManager;

public void update(Order order){
updateInvoice();
mergeInvoice(Order order);
}

public void updateInvoice(){
entitymanager.createQuery("update Orders set status = 'ERROR' where id=12").executeUpdate();
}

public void mergeInvoice(Order order){
entitymanager.merge(order);
    }


}

In the above, merge happens before executeUpdate(); even though there are several transactions in between execute update is the last transaction which occurs. WHY?

I tried entityManager.flush(); but it didn't work.

Kiran Kumar
  • 748
  • 5
  • 11
  • 34
  • post the code calling the mentioned methods. – Samuel Kok Sep 22 '16 at 02:19
  • Not sure about your code, but your question about `merge` vs. `update`: http://stackoverflow.com/questions/1069992/jpa-entitymanager-why-use-persist-over-merge *"Merge creates a new instance of your entity, copies the state from the supplied entity, and makes the new copy managed."* – markspace Sep 22 '16 at 02:28

1 Answers1

0

I resolved this by calling the update and merge methods from service layer. I found that for the transaction to get commit I have to return out of my DAO.

public void update(Order order){
orderDao.updateInvoice();
orderDao.mergeInvoice(Order order);
}
Kiran Kumar
  • 748
  • 5
  • 11
  • 34