我正在 Spring Boot 应用程序中使用JPA。
我才刚刚开始使用它,我有一些问题。
我的DAO代码如下:
@Transactional
public class DatabaseUnitDao implements IDatabaseUnitDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public void create(Unit unit) {
final String CREATE_UNIT =
"CREATE TABLE " + unit.getName() + " (id VARCHAR(255) PRIMARY KEY NOT NULL, value text NOT NULL)";
entityManager.persist(unit); // add an info about unit in the general table
entityManager.createNativeQuery(CREATE_UNIT).executeUpdate(); // create table for this units
}
}
1.在这种情况下我应该使用flush()
吗?
2.只用@Transactional注解DAO类就够了吗?
一些资源表明需要使用@EnableTransactionManagement才能使用@Transactional。
最佳答案
没有。 JPA 提供程序必须在事务方法调用结束时为您完成此操作。
是的。 Spring Boot 默认启用事务管理(proxyTargetClass = true)
关于java - 如果我已经在使用@Transactional,我应该什么时候使用entityManager.flush(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54836438/