Home > Software design >  EntityManager is blocking the database trigger
EntityManager is blocking the database trigger

Time:11-05

In the scenario I prepared, jpa locks the new record. Therefore, the trigger in the database is not activated. How can I resolve this situation?

When I create customer, the code is assigned to the customer by the trigger. However, since the record created during this process is locked, the trigger is not activated.

@Entity
public class Customer {
    @Id
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private String address;
    @Column
    private int personRef;
    @Column
    private int customerCode; //db trigger updated
}

@Entity
public class Person {
    @Id
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private String surname;

}


public class CustomerDTO {

    private int id;
    private String name;
    private String address;
    private int personRef;
    private PersonDTO person;
}


public class PersonDTO {
    private int id;
    private String name;
    private String surname;

}

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer>{
    
}

@Service
public class CustomerService{
    @Autowired
    private final CustomerRepository customerRepository;
    @Autowired
    private final PersonService personService;
    
    @Transactional(rollbackFor = {Exception.class})
    public int control(CustomerDTO customerDTO){
         Customer customer =  customerRepository.findById(customerDTO.getId());
         if(customer == null){
             ModelMapper mapper = new ModelMapper();
              customer =  mapper.map(customer,Customer.class);
             customerRepository.saveAndFlush(customer);
             Person person = mapper.map (customer.getPerson(),Person.class);
             personService.saveAndFlush(person);
             customer =  customerRepository.findById(customerDTO.getId());
             customer.setPersonRef(person.getId());
             customerRepository.saveAndFlush(customer);
             return customer.getCustomerCode;
             
         }
         return customer.getCustomerCode();
    
    }
    
}

CodePudding user response:

The problem is that the trigger runs in the database and Hibernate doesn't know about that.

So you have to refresh the entity after the trigger runs:

@Service
public class CustomerService{
    @Autowired
    private final CustomerRepository customerRepository;
    @Autowired
    private final PersonService personService;
    @Autowired
    private EntityManager em;

    @Transactional(rollbackFor = {Exception.class})
    public int control(CustomerDTO customerDTO){
         Customer customer =  customerRepository.findById(customerDTO.getId());
         if(customer == null){
             ModelMapper mapper = new ModelMapper();
              customer =  mapper.map(customer,Customer.class);
             customerRepository.saveAndFlush(customer);
             Person person = mapper.map (customer.getPerson(),Person.class);
             personService.saveAndFlush(person);
             customer =  customerRepository.findById(customerDTO.getId());
             customer.setPersonRef(person.getId());
             customerRepository.saveAndFlush(customer);

             // Reload the data from the database
             em.refresh(customer);

             return customer.getCustomerCode;
             
         }
         return customer.getCustomerCode();
    
    }
    
}
  • Related