gpt4 book ai didi

java - 为什么表用户没有创建?我收到此消息 "Unsuccessful: create table user"

转载 作者:行者123 更新时间:2023-12-01 07:51:14 26 4
gpt4 key购买 nike

我有一个 Spring-boot 项目。对不起那些英语不好的人。所以,问题是:当我通过“bootRun”运行我的项目时,3 个表中的 2 个仅在我的 postgresql 数据库中创建。它们是“角色”和“用户角色”。当 Hibernate 尝试创建第三个表“user”时,它会返回一条消息。这是消息:enter image description here

这是user.java代码

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@NotEmpty
private String name;

@NotEmpty
@Column(unique = true, nullable = false)
private String login;

@NotEmpty
private String password;

@JsonIgnore
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
private Set<Role> roles = new HashSet<Role>();

public User() {
}

public User(User user) {
super();
this.id = user.getId();
this.name = user.getName();
this.login = user.getLogin();
this.password = user.getPassword();
this.roles = user.getRoles();
}

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Set<Role> getRoles() {
return roles;
}

public void setRoles(Set<Role> roles) {
this.roles = roles;
}

}

这是role.java代码

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.security.core.GrantedAuthority;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class Role implements GrantedAuthority {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@NotEmpty
private String name;

@JsonIgnore
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
private Set<User> users = new HashSet<User>();

@Override
public String getAuthority() {
return name;
}

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<User> getUsers() {
return users;
}

public void setUsers(Set<User> users) {
this.users = users;
}

public Role(String name, Set<User> users) {
this.name = name;
this.users = users;
}

public Role() {
}
}

application.properties文件

spring.profiles.active=dev

spring.datasource.url= jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=12345

spring.jpa.hibernate.ddl-auto=create

和build.gradle

buildscript {
repositories {
mavenCentral()
maven { url "https://repo.spring.io/plugins-release" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RC1")
}
}

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "spring-boot"
apply plugin: "war"

jar {
baseName = "spring-rest-service-oauth"
version = "0.1.0"
}

repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
}

dependencies {
// compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile 'postgresql:postgresql:9.1-901-1.jdbc4'

compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.security.oauth:spring-security-oauth2")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("com.jayway.jsonpath:json-path")
testCompile("com.jayway.jsonpath:json-path-assert")
}

task wrapper(type: Wrapper) {
gradleVersion = "2.7"
}

感谢回复

最佳答案

因为User是PostgreSQL中的保留关键字。

关于java - 为什么表用户没有创建?我收到此消息 "Unsuccessful: create table user",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196802/

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