gpt4 book ai didi

java - 搞乱 JPA 并收到错误创建用户 - 无法设置字段值

转载 作者:行者123 更新时间:2023-11-29 19:47:41 28 4
gpt4 key购买 nike

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

enter image description here

我已按如下方式设置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"
}

我收到此错误:

Error creating the user: org.springframework.orm.jpa.JpaSystemException: Could not set field value [ff80818158b79f8e0158b7a285b60001] value by reflection : [class com.test.models.User.id] setter of com.test.models.User.id; nested exception is org.hibernate.PropertyAccessException: Could not set field value [ff80818158b79f8e0158b7a285b60001] value by reflection : [class com.test.models.User.id] setter of com.test.models.User.id

最佳答案

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

尝试:

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

关于java - 搞乱 JPA 并收到错误创建用户 - 无法设置字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40900547/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com