gpt4 book ai didi

java - 没有本地生成的身份值

转载 作者:行者123 更新时间:2023-12-02 08:42:33 26 4
gpt4 key购买 nike

我是 springframework 技术的新手,我正在努力解决以下错误。项目启动成功,执行 postman 时出现以下错误谁能帮我解决这个问题。我想我已经粘贴了重要的类,如果需要我可以发布整个代码

用户 Controller

package com.appsdeveloper.blog.app.ws.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.appsdeveloper.blog.app.ws.service.UserService;
import com.appsdeveloper.blog.app.ws.shared.dto.UserDto;
import com.appsdeveloper.blog.app.ws.ui.model.request.UserDetailsRequestModel;
import com.appsdeveloper.blog.app.ws.ui.model.response.UserRest;

@RestController
@RequestMapping("users")//http://localhost:8586/users

public class UserController{

@Autowired(required=true)//the bracket added from the internet
UserService userService;

//@Autowired
//private UserRepository userRepository;

@PostMapping
public UserRest createUser(@RequestBody UserDetailsRequestModel userDetails){

UserRest returnValue = new UserRest();

UserDto userDto = new UserDto();
BeanUtils.copyProperties(userDetails, userDto);

UserDto createdUser = userService.createUser(userDto);
BeanUtils.copyProperties(createdUser, returnValue);

return returnValue;
}

@DeleteMapping
public String deleteUser(){
return "delete user was called";
}

@GetMapping
public String getUser(){
return "get User was called";
}

@PutMapping
public String updateUser(){
return "update user was called";
}
}

用户实体

package com.appsdeveloper.blog.app.ws.io.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users", schema = "photo_app" )//name of table created to store information
public class UserEntity implements Serializable {

private static final long serialVersionUID = 7401188933477397731L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="Id")
private long id;

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

@Column(name = "firstName", nullable = false, length = 50)//the length is important to avoid default size 0f 250 varchar
private String firstName;

@Column(name = "lastName", nullable = false, length = 50)
private String lastname;

@Column(name = "email", length = 50)
private String email;

@Column(name = "password", nullable = false)
private String encryptedPassword;

//@Column(name = "emailVerificationToken", nullable = false)
private String emailVerificationToken;


@Column(name = "emailVerificationStatus", nullable=false)
private Boolean emailVerificationStatus = false;


public long getId() {
return id;
}
public String getUserId() {
return userId;
}
public String getFirstName() {
return firstName;
}
public String getLastname() {
return lastname;
}
public String getEmail() {
return email;
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public String getEmailVerificationToken() {
return emailVerificationToken;
}

public Boolean getEmailVerificationStatus() {
return emailVerificationStatus;
}



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

public void setUserId(String userId) {
this.userId = userId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}

public void setEmail(String email) {
this.email = email;
}

public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}

public void setEmailVerificationToken(String emailVerificationToken) {
this.emailVerificationToken = emailVerificationToken;
}

public void setEmailVerificationStatus(boolean emailVerificationStatus) {
this.emailVerificationStatus = emailVerificationStatus;
}

}


应用程序属性

spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/photo_app
server.port=8586
spring.jpa.hibernate.use-new-id-generator-mappings=false


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.appsdeveloperblog-app-ws</groupId>
<artifactId>mobile-app-ws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mobile-app-ws</name>
<description>Demo project for Spring Boot</description>

<properties><!--</tiles:insertDefinition>-->
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>-->
<java.version>1.8</java.version>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha4</version>
<type>pom</type>
</dependency>

<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jadira.usertype/usertype.core -->
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>7.0.0.CR1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

错误代码

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: The database returned no natively generated identity value; nested exception is org.hibernate.HibernateException: The database returned no natively generated identity value] with root cause

org.hibernate.HibernateException: The database returned no natively generated identity value

最佳答案

您必须自动递增数据库中的id列。

类似这样的:id INT NOT NULL AUTO_INCRMENT

关于java - 没有本地生成的身份值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61283355/

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