【发布时间】:2021-12-19 11:13:43
【问题描述】:
在我准备的场景中,jpa 锁定了新记录。因此,数据库中的触发器没有被激活。我该如何解决这种情况?
当我创建客户时,代码由触发器分配给客户。但是,由于在此过程中创建的记录被锁定,因此触发器没有被激活。
@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();
}
}
【问题讨论】:
-
我不明白你的问题。为什么你认为有东西被锁定了? SaveAndFlush 不会从数据库中读取记录,因此 customerCode 不包含您在触发器中设置的值
-
我用 saveAndFlush 保存的记录用数据库端的触发器更新。当我第二次尝试提取刚刚使用 findById 创建的记录时,触发器更新的列显示为空。我认为 Hibernate 正在锁定我创建的记录。我找不到如何解除这个锁。
-
No findById 从一级缓存返回实体。您必须调用 refresh 才能从数据库中获取实体 newley
-
jpa 该怎么办。
标签: hibernate jpa spring-data-jpa