【问题标题】:PostMagging with spring boot java to mysqlPostMagging 与 spring boot java 到 mysql
【发布时间】:2021-06-01 18:25:44
【问题描述】:

下面的 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> {}

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    你得到的休眠错误:

    org.hibernate.exception.ConstraintViolationException
    

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

    userRepository.save(userDetails);
    

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

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

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

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

    【讨论】:

      【解决方案2】:

      我在 post 对象中来宾的 firstName 或 lastName 为空。由于您使用 @Valid 注释验证对象,因此还要确保在必填字段上添加 @NotNull 以在反序列化期间验证它们

      【讨论】:

        猜你喜欢
        • 2021-06-05
        • 2020-07-12
        • 2021-03-20
        • 1970-01-01
        • 1970-01-01
        • 2018-07-10
        • 2019-07-20
        • 2018-02-07
        • 2021-10-23
        相关资源
        最近更新 更多