我在我服务的方法:春 - 无法赶上ConstraintViolationException
@Transactional
public MyResponse create(UserCredentials cred) {
User user = new User(cred);
try {
final User created = userRepository.save(user);
return new MyResponse(user, "Created");
} catch (TransactionSystemException e) {
return new MyResponse(null, "Cannot create");
} catch (ConstraintViolationException e) {
return new MyResponse(null, "Cannot create");
}
}
用户等级:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(
name = "id",
updatable = false,
nullable = false
)
private Long id;
@NotBlank
@Column(
nullable = false,
unique = true
)
private String username;
@NotBlank
@Column(
nullable = false,
unique = true
)
private String email;
@NotBlank
@Column(nullable = false)
private String password;
而且问题是,当我在JSON空的用户名或空的邮件发送。当调试应用程序,我得到ConstraintViolationExcpetion,但在JSON响应,我得到:
{
"timestamp": 1500026388864,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.transaction.TransactionSystemException",
"message": "Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly",
"path": "/register"
}
调试器是在抓(ConstraintViolationException E)停止,但最后我得到SystemTransactionException,为什么呢?
可能的复制(https://stackoverflow.com/questions/13777666/no-rollback-for-constraintviolationexception-in-transactional-service) – xyz