Using entityManager "merge" method

Hello, I am trying to update a record via entityManager but can’t get it to work.
My code below is called from an EntityListener, by any of the After… listeners.

Two questions:

  1. When I use persistence.createTransaction rather than persistence.getTransaction, my app hangs - why is that?
  2. The code below doesn’t update the record (as far as I can see…). What is the proper way of using ‘merge’?

Thanks,


/*
 * Copyright (c) 2016 toj
 */
package com.company.toj.listener;

import javax.inject.Inject;

import com.company.toj.entity.HrEvent;
import com.company.toj.entity.Person;
import com.company.toj.service.HrEventService;

import org.springframework.stereotype.Component;

import com.haulmont.cuba.core.EntityManager;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.Transaction;
import com.haulmont.cuba.core.listener.AfterDeleteEntityListener;
import com.haulmont.cuba.core.listener.AfterInsertEntityListener;
import com.haulmont.cuba.core.listener.AfterUpdateEntityListener;

/**
 * @author matth
 */
@Component("toj_HrEventAfterEntityListener")
public class HrEventAfterEntityListener implements AfterDeleteEntityListener<HrEvent>, AfterInsertEntityListener<HrEvent>, AfterUpdateEntityListener<HrEvent> {
	

	@Inject
	private Persistence persistence;
 
 
    public void onAfterSomechange(HrEvent hrEvent) {
    	
    	Person person = hrEvent.getPerson();
    	
    	Transaction tx = persistence.getTransaction();
 		try {
 			EntityManager em = persistence.getEntityManager(); 
		 	Person loadedPerson = em.merge(person);
		 	
		 	loadedPerson.setCurrentHrBaseFee(7777);
            
            tx.commit();
            
 		} finally {
 			tx.end();
 		}              
    }    

    @Override
    public void onAfterDelete(HrEvent entity) {
    onAfterSomechange(entity);
    }


    @Override
    public void onAfterInsert(HrEvent entity) {
    onAfterSomechange(entity);
    }


    @Override
    public void onAfterUpdate(HrEvent entity) {
    onAfterSomechange(entity);
    }
}

Hi Matthis,
You cannot modify the current persistence context in After-listeners, this is mentioned in the documentation. It means that you shouldn’t use EntityManager from the current transaction.

1) When I use persistence.createTransaction rather than persistence.getTransaction, my app hangs - why is that?
This is probably because the new transaction waits until the current transaction which updates the same records commits.

2) The code below doesn’t update the record (as far as I can see…). What is the proper way of using ‘merge’?
When you call getTransaction(), you use the current one, but operations through EntityManager do not take effect because the persistence context is already flushed and cannot be used anymore.

To avoid the above problems, just use Before-listeners. See cuba platform - Create Entity by using EntityListener - Stack Overflow for example.

Hi Konstatin, thank you very much for your reply, I now understand.
The entity listener example linked was also useful.

Best regards