简体   繁体   English

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

[英]Could not commit JPA transaction: nested exception is javax.persistence.RollbackException: Error while committing the transaction

I looked at multiple similar instances (mentioned below) where the error is almost same but I found that my scenario is a bit different.我查看了多个类似的实例(如下所述),其中错误几乎相同,但我发现我的场景有点不同。 Can someone please help me with this?有人可以帮我吗?

In my case the exact exception message is -在我的例子中,确切的异常消息是 -

Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction

This issue occurs when I try to save MarriagePerson entity which is in @ManyToOne relation with User .当我尝试保存与User存在@ManyToOne关系的MarriagePerson实体时,会出现此问题。 Please find both the entities and their relations below -请在下面找到实体及其关系 -

User.java用户.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 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, max = 20, message = "Name must be between 2 and 20 characters long")
    @Column(name = "firstName")
    private String firstName;
}

Here is what exactly I am doing -这就是我正在做的 -

  • I have saved one User in my postgres db.我在我的postgres数据库中保存了一个User
  • Now I am trying to save MarriagePerson for the same User then the above mentioned exception is caught.现在我正在尝试为同一个User保存MarriagePerson ,然后捕获到上述异常。

Please find the UserSerive and MarriagePersonService below -请在下面找到UserSeriveMarriagePersonService -

UserService.java UserService.java

@Service
public class UserService implements IUserService {

    @Autowired
    private IUserRepository userRepository;
    
    @Override
    public User save(User user) {
        return userRepository.save(user);
    }
}

MarriagePerson.java 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;
    }
}

Exception is caught at this line marriagePersonRepository.save(marriagePerson);在此行捕获异常marriagePersonRepository.save(marriagePerson); for following mentioned request payload-对于以下提到的请求有效负载-

MarriagePerson payload MarriagePerson 负载

{
  "firstName": "Name",
  "gender": "Male"
}

Saved User data is below (which we are trying to update MarriagePerson for)保存的用户数据如下(我们正在尝试更新 MarriagePerson)

{
    "firstName": "string",
    "middleName": "string",
    "lastName": "string",
    "email": "string@string.com",
    "alternateEmail": "string1@string.com",
    "mobile": "0123456789",
    "alternateMobile": "0123456789",
    "password": "string",
    "marriagePerson": []
}

EDIT编辑

Repository interfaces are -存储库接口是 -

IMarriagePersonRepository.java IMarriagePersonRepository.java

@Repository
public interface IMarriagePersonRepository
        extends JpaRepository<MarriagePerson, Long>, JpaSpecificationExecutor<MarriagePerson> {
    List<MarriagePerson> findAll();
}

IUserRepository.java IUserRepository.java

@Repository
public interface IUserRepository extends JpaRepository<User, Long> {
    public User findByMobile(String mobile);
    User findByMobileAndEmail(String mobile, String email);
}

Thank you in advance.先感谢您。

I had the same issue, and the exception message was not helpful.我有同样的问题,异常消息没有帮助。 In my case, I was trying to save the encoded password into a column of size 20.就我而言,我试图将编码后的密码保存到大小为 20 的列中。

@Size(max = 20) // not enough for an encoded password
private String password;

But the encoded password length was more than that, so once I increased the size of the column in the entity to 120 it worked like a charm.但是编码后的密码长度不止于此,所以一旦我将实体中列的大小增加到 120,它就很有魅力了。

So I would suggest to anyone who is facing this issue, to double-check the data length that you are trying to save in the database table.因此,我建议遇到此问题的任何人仔细检查您尝试保存在数据库表中的数据长度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.persistence.RollbackException:在JPA代码中提交事务时出错 - javax.persistence.RollbackException : Error while committing the transaction in JPA code javax.persistence.RollbackException:提交事务时出错 - javax.persistence.RollbackException: Error while committing the transaction Hibernate javax.persistence.RollbackException:提交事务时出错 - Hibernate javax.persistence.RollbackException: Error while committing the transaction javax.persistence.RollbackException:提交事务时出错(没有 Spring 的 JPA) - javax.persistence.RollbackException: Error while committing the transaction (JPA without Spring) org.springframework.transaction.TransactionSystemException:无法提交JPA事务是javax.persistence.RollbackException - org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction is javax.persistence.RollbackException javax.persistence.RollbackException:使用根目录提交事务时出错] java.lang.StackOverflowError:null - javax.persistence.RollbackException: Error while committing the transaction] with root cause java.lang.StackOverflowError: null Jpa事务javax.persistence.RollbackException:事务标记为rollbackOnly - Jpa transaction javax.persistence.RollbackException: Transaction marked as rollbackOnly 线程“主”中的异常javax.persistence.RollbackException:事务标记为rollbackOnly - Exception in thread “main” javax.persistence.RollbackException: Transaction marked as rollbackOnly javax.persistence.RollbackException:事务标记为rollbackOnly - javax.persistence.RollbackException: Transaction marked as rollbackOnly 无法提交 JPA 事务/提交事务时出错/NullPointerException - Could not commit JPA transaction / Error while committing the transaction / NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM