首页 > 解决方案 > PostMagging 与 spring boot java 到 mysql

问题描述

下面的 java spring boot 代码表示对 java 后端的简单 rest 调用,它在 mysql 数据库上执行插入,但是当它执行 rest 调用时,我报告了下面的错误,插入时我无法定义问题出在哪里mysql db,如何解决控制台中hibernate表示的错误?谢谢你

错误消息休眠:

    "could not execute statement; SQL [n/a]; constraint [null]; 
nested exception is org.hibernate.exception.ConstraintViolationException: 
could not execute statement

控制器:

 @PostMapping("/newuser")
    public User createUser(@Valid @RequestBody User userDetails) {
        System.out.println("Sono nella creazione dell'utente");
        return userRepository.save(userDetails);
    }

模型:

@Entity
@Table(name = "users")
@EntityListeners(AuditingEntityListener.class)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private long id;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name", nullable = false)
    private String lastName;

    @Column(name = "email_address", nullable = true)
    private String email;

    /**
     * Gets id.
     *
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * Sets id.
     *
     * @param id the id
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * Gets first name.
     *
     * @return the first name
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets first name.
     *
     * @param firstName the first name
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Gets last name.
     *
     * @return the last name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets last name.
     *
     * @param lastName the last name
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Gets email.
     *
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets email.
     *
     * @param email the email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Gets created at.
     *
     * @return the created at
     */

    /**
     * Sets created at.
     *
     * @param createdAt the created at
     */

    /**
     * Gets created by.
     *
     * @return the created by
     */


    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                '}';
    }


}

存储库

import xxx.mode.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
 * The interface User repository.
 *
 * @author xxxx
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}

标签: javaspringspring-boot

解决方案


您得到的休眠错误:

org.hibernate.exception.ConstraintViolationException

仅表示您违反了数据库约束并查看您的User实体,您有一些约束,例如firstNamelastName不应为空。因此,当下面的行执行时,您会收到这些错误:

userRepository.save(userDetails);

您确定这些字段不为空吗?

此外,您正在使用@Valid注释,但我没有看到javax.validation.constraints您的User实体上使用了任何注释,因此,当您的 API 被调用时,实际上没有发生任何验证。我可以向你推荐的是:

  1. 在您的字段中使用javax.validations诸如@NotBlank(message = "First name is required" )。这将在它到达您的 API 时引发验证错误,并且不会继续userRepository.save(userDetails). 或者更好地为您的请求正文创建一个 DTO 类,而不是使用实体 - 这是一种代码味道,但我猜这只是为了测试目的。

  2. 此外,您可以省略字段上的@Column声明,private long id因为它已经用@Id进行了注释。主键已经是唯一的,不能修改。


推荐阅读