spring引导java到mysql的后期标记

4zcjmb1e  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(315)

下面的java spring引导代码表示对java后端的一个简单rest调用,该调用在mysql数据库上执行插入操作,但是当它执行rest调用时,我报告了下面的错误,我无法定义在mysql数据库中插入时的问题在何处,如何解决控制台中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> {}
kgsdhlau

kgsdhlau1#

出现休眠错误:

org.hibernate.exception.ConstraintViolationException

这只意味着您违反了数据库约束,并且在查看用户实体时,您有一些约束,例如firstname和lastname不应为null。因此,在执行下一行时,您将得到这些错误:

userRepository.save(userDetails);

您确定这些字段不是空的吗?
你也在使用@valid注解,但我没有看到任何注解 javax.validation.constraints 因此,在调用api时,实际上不会进行验证。我可以向你推荐的是:
使用 javax.validations 例如字段上的@notblank(message=“first name is required”)。这将在遇到api时抛出验证错误,并且不会继续 userRepository.save(userDetails) . 或者最好为您的请求体创建一个dto类,而不是使用实体-这是一种代码味道,但我猜这只是出于测试目的。
此外,您还可以省略 private long id 字段,因为它已经用@id注解。主键已经是唯一的,不能修改。

vulvrdjw

vulvrdjw2#

post对象中的guest firstname或lastname为空。因为您使用@valid注解验证对象,所以还要确保在反序列化期间在必需字段上添加@notnull来验证它们

相关问题