Alex Moiseenkov Alex Moiseenkov - 6 months ago 17
Java Question

why table User not creat ? I get this msg " Unsuccessful: create table user"

I have a project in Spring-boot.
Sorry for bad English guys.
So, problem is: when i run my project by "bootRun", 2 of 3 tables only created in my postgresql database. They are "role" and "user_role". And the Hibernate return a message, when it try to create the third table "user". This is the message:enter image description here

this is user.java code

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;
}

}


this is role.java code

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 file

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


and 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"
}


thanks who respond

Answer

Because of User is a reserved keyword in PostgreSQL.

Comments