spring 乱用JPA并获得创建用户时出错-无法设置字段值

k5ifujac  于 2022-10-30  发布在  Spring
关注(0)|答案(3)|浏览(134)

我正在按照Spring教程开始使用JPA,我有一个基本的mysql数据库和一个user表。

我已经按如下方式设置了User.java

package com.test.models;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Id;
import java.util.UUID;

@Entity
@Table(name="user")
public class User {

    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    @Column(name="id")
    private UUID id;

    @Column(name="first_name")
    String first_name;

    @Column(name="last_name")
    String last_name;

    @Column(name="username")
    String username;

    public User(String first_name, String last_name, String username) {
        this.first_name = first_name;
        this.last_name = last_name;
        this.username = username;
    }

    public User() { } 

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

我已经设置了UserRepo.java

package com.test.models;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.UUID;

@RepositoryRestResource
public interface UserRepo extends CrudRepository<User, UUID>{
    User findByUsername(String username);

}
最后是UserController.java

package com.test.controllers;

import com.test.models.UserRepo;
import com.test.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    private UserRepo userRepo;

    @RequestMapping("/create")
    @ResponseBody
    public String create(String first_name, String last_name, String username) {
        String userId = "";
        try {
            User user = new User(first_name, last_name, username);
            userRepo.save(user);
            userId = String.valueOf(user.getId());
        }
        catch (Exception ex) {
            return "Error creating the user: " + ex.toString();
        }
        return "User successfully created with id = " + userId;
    }

}

我还设置了数据库连接:

spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=fakepass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

但是,当我使用postman对localhost:8080/create进行POST时,

{
    "first_name": "Jim",
    "last_name": "Smith",
    "username": "jim.smith"
}

我得到这个错误:
创建用户时出错:org.springframework.orm.jpa.JpaSystemException:无法通过反射设置字段值[ff 80818158 b79 f8 e0158 b7 a285 b60001]的值:[类com.test.models.User.id] com.test.models.User.id的设置者;嵌套的异常是组织的休眠。无法通过反射设置字段值[ff 80818158 b79 f8 e0158 b7 a285 b60001]的值:[类别com.test.models.User.id] com.test.models.User.id的设定者

gkn4icbw

gkn4icbw1#

我遇到了同样的问题。您在@GenericGenerator中的策略设置不正确。
请尝试:

@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
r7s23pms

r7s23pms2#

所有JPA实体类都应具有空构造函数,而您的User类缺少空构造函数,这是导致此问题的原因,因此请按如下所示更改您的User实体类并添加User()构造函数:

public class User {

    public User() {//empty constructor
    }

    //add existing code here

}

实体类必须具有无参数构造函数。实体类也可以具有其他构造函数。无参数构造函数必须是公共的或受保护的。
您可以在here中查找该规范。

67up9zun

67up9zun3#

在数据库中存在UUID类型,请尝试使用UUID而不是varchar(36)。

相关问题