如何解决无法提交JPA事务:嵌套异常为javax.persistence.RollbackException:提交事务时出错
我查看了多个相似的实例(如下所述),它们的错误几乎相同,但是我发现我的情况有些不同。有人可以帮我吗?
- javax.persistence.RollbackException: Transaction marked as rollbackOnly
- Could not commit JPA transaction: Transaction marked as rollbackOnly
- 还有更多。
在我的情况下,确切的异常消息是-
Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
当我尝试保存与MarriagePerson
关系的@ManyToOne
实体时,会发生此问题
User
。请在下面找到实体及其关系-
User.java
@Entity
@Table(name = "MAIN_USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OnetoMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "user")
private Collection<MarriagePerson> marriagePerson = new ArrayList<MarriagePerson>();
@NotEmpty(message = "Name can not be empty")
@Size(min = 2,max = 20,message = "Name must be between 2 and 20 characters long")
@Column(name = "first_name")
private String firstName;
@Column(name = "middle_name")
private String middleName;
@Column(name = "last_name")
private String lastName;
@NotEmpty(message = "Email can not be empty")
@Size(min = 5,message = "Email must be more than 5 characters long")
@Column(name = "email",unique = true)
private String email;
@Column(name = "alternateEmail")
private String alternateEmail;
@NotEmpty(message = "Mobile can not be empty")
@Size(min = 10,max = 10,message = "Mobile must be 10 digits long")
@Column(name = "mobile",unique = true)
private String mobile;
@Column(name = "alternate_mobile")
private String alternateMobile;
@NotEmpty(message = "Password can not be empty")
@Size(min = 6,message = "Password must be more than 6 characters long")
@Column(name = "password")
private String password;
}
MarriagePerson.java
@Entity
@Table(name = "MARRIAGE_PERSON")
public class MarriagePerson {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(columnDeFinition = "user_id")
private User user;
@NotEmpty
@Column(name = "gender")
private String gender;
@NotEmpty(message = "Name can not be empty")
@Size(min = 2,message = "Name must be between 2 and 20 characters long")
@Column(name = "firstName")
private String firstName;
}
这正是我在做什么-
请在下面找到UserSerive
和MarriagePersonService
-
UserService.java
@Service
public class UserService implements IUserService {
@Autowired
private IUserRepository userRepository;
@Override
public User save(User user) {
return userRepository.save(user);
}
}
MarriagePerson.java
@Service
public class MarriagePersonService implements IMarriagePersonService {
@Autowired
private IMarriagePersonRepository marriagePersonRepository;
@Override
public MarriagePerson save(MarriagePerson marriagePerson) {
userRepository.findById(userId).map(user -> {
marriagePerson.setUser(user);
return marriagePersonRepository.save(marriagePerson);
});
return marriagePerson;
}
}
在此行marriagePersonRepository.save(marriagePerson);
处捕获了以下提到的请求有效负载的异常-
MarriagePerson有效载荷
{
"firstName": "Name","gender": "Male"
}
已保存的用户数据在下面(我们正在尝试为此更新MarriagePerson)
{
"firstName": "string","middleName": "string","lastName": "string","email": "string@string.com","alternateEmail": "string1@string.com","mobile": "0123456789","alternateMobile": "0123456789","password": "string","marriagePerson": []
}
编辑
存储库接口为-
IMarriagePersonRepository.java
@Repository
public interface IMarriagePersonRepository
extends JpaRepository<MarriagePerson,Long>,JpaSpecificationExecutor<MarriagePerson> {
List<MarriagePerson> findAll();
}
IUserRepository.java
@Repository
public interface IUserRepository extends JpaRepository<User,Long> {
public User findByMobile(String mobile);
User findByMobileAndEmail(String mobile,String email);
}
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。