我查看了多个类似的示例(下面提到),错误几乎相同,但我发现我的场景有点不同。有人能帮我吗?
- 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
尝试保存与User
具有@ManyToOne
关系的MarriagePerson
实体时出现此问题。请在下面找到实体及其关系-
用户. 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;
}
婚姻伴侣. 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, max = 20, message = "Name must be between 2 and 20 characters long")
@Column(name = "firstName")
private String firstName;
}
我是这么做的-
- 我已经在我的
postgres
数据库中保存了一个User
。 - 现在,我试图为同一个
User
保存MarriagePerson
,然后捕获上述异常。
请在下面查找UserSerive
和MarriagePersonService
-
用户服务. java
@Service
public class UserService implements IUserService {
@Autowired
private IUserRepository userRepository;
@Override
public User save(User user) {
return userRepository.save(user);
}
}
婚姻伴侣. 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);
这一行捕获以下提到的请求有效负载的异常-
婚姻人员有效负载
{
"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": []
}
编辑
存储库接口包括-
我的婚姻个人存储库. java
@Repository
public interface IMarriagePersonRepository
extends JpaRepository<MarriagePerson, Long>, JpaSpecificationExecutor<MarriagePerson> {
List<MarriagePerson> findAll();
}
I用户存储库. java
@Repository
public interface IUserRepository extends JpaRepository<User, Long> {
public User findByMobile(String mobile);
User findByMobileAndEmail(String mobile, String email);
}
先谢谢你。
1条答案
按热度按时间knsnq2tg1#
我也遇到了同样的问题,异常消息没有帮助。在我的例子中,我试图将编码后的密码保存到大小为20的列中。
但是编码的密码长度比这个长,所以一旦我把实体中列的大小增加到120,它就像一个护身符一样工作了。
因此,我建议任何面临这个问题的人,仔细检查您试图保存在数据库表中的数据长度。