无法提交JPA事务:嵌套异常为javax.persistence.RollbackException:提交事务时出错

问题描述

我查看了多个相似的实例(如下所述),它们的错误几乎相同,但是我发现我的情况有些不同。有人可以帮我吗?

在我的情况下,确切的异常消息是-

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;
}

这正是我在做什么-

  • 我在User数据库中保存了一个postgres
  • 现在,我尝试将MarriagePerson保存为相同的User,然后捕获到上述异常。

请在下面找到UserSeriveMarriagePersonService-

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 (将#修改为@)