0

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. Please find both the entities and their relations below -

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, 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.
  • Now I am trying to save MarriagePerson for the same User then the above mentioned exception is caught.

Please find the UserSerive and MarriagePersonService below -

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

Exception is caught at this line marriagePersonRepository.save(marriagePerson); for following mentioned request payload-

MarriagePerson payload

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

Saved User data is below (which we are trying to update MarriagePerson for)

{
    "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

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

Thank you in advance.

Tilak Dewangan
  • 351
  • 1
  • 2
  • 19

1 Answers1

0

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.

@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.

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.

Luay Abdulraheem
  • 751
  • 3
  • 11