突出显示的文本告诉select select在事务内部启动,但不是在事务提交后发生的insert语句。
我的想法是,我希望查询的所有内容都在事务内部。
这些是我在控制器,存储库和映射中的代码。
控制器动作方法
@Transactional(readOnly = false,rollbackFor = Exception.class,
isolation = Isolation.READ_COMMITTED)
@RequestMapping(value = "/{userId}role", method = RequestMethod.PUT)
public void AddUserToRole(@PathVariable int userId,
@RequestBody AddUserToRoleRequest request) {
roleRepository.getCurrentFlushMode();
User user = userRepository.getUserById(userId);
Role role = roleRepository.getRoleById(request.getRoleId());
role.getUsers().add(user);
user.getRoles().add(role);
}
RoleRepository
@Repository
public class RoleRepository {
private static Log log = LogFactory.getLog(RoleRepository.class);
@PersistenceContext
EntityManager entityManager;
public void addRole(Role role) {
entityManager.persist(role);
}
public Role getRoleById(long roleId) {
Role role = entityManager.find(Role.class, roleId);
return role;
}
public void getCurrentFlushMode() {
log.info(String.format("current flush mode %s", entityManager.getFlushMode()));
}
}
UserRepository
@Repository
public class UserRepository {
@PersistenceContext
private EntityManager entityManager;
public void addUser(User user){
entityManager.persist(user);
}
public User getUserById(long userId){
User user = entityManager.find(User.class,userId);
return user;
}
}
角色实体
@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY
//,cascade = {CascadeType.PERSIST, CascadeType.MERGE,}
)
@JoinTable(name = "user_roles",
joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<User> users;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
用户实体
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String email;
@OneToMany(
// cascade = CascadeType.ALL,
mappedBy = "user",
fetch = FetchType.LAZY)
private List<Post> posts;
@ManyToMany(mappedBy = "users"
//, cascade = CascadeType.ALL
)
private List<Role> roles;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getId() {
return id;
}
public List<Post> getPosts() {
return posts;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public void addRole(Role role) {
role.getUsers().add(this);
this.getRoles().add(role);
}
}
如果我显式调用enityManager.flush();此问题按预期在事务内插入语句。 Hower我只是不想显式调用flush因为插入新用户或角色我不需要调用flush并且插入发生在事务begin / commit中。
非常感谢