Discuss / Java / 感觉一个超级不合理的地方,hibernate居然必须开始事务才能执行对应sql

感觉一个超级不合理的地方,hibernate居然必须开始事务才能执行对应sql

Topic source

Loading...

#1 Created at ... [Delete] [Delete and Lock User]

这里必须要有@Transactional注解,hibernate才能执行对应sql方法

@Component
@Transactional
public class UserService {
    @Autowired
    HibernateTemplate hibernateTemplate;
    public User register(String name,String password,String email){
        User user=new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setName(name);
//        hibernateTemplate.persist(user);
        hibernateTemplate.save(user);
        return user;
    }
}

不然就会报

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

  • 1

Reply