我正在尝试使用Spring和Postgres创建一个应用程序。目前,我有3个实体:User
,Role
和UserRole
。
用户
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Builder
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false)
private Long id;
@Column(name = "username", nullable = false, unique = true)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "name", nullable = false)
private String firstName;
@Column(name = "lastName", nullable = true)
private String lastName;
@Column(name = "gender", nullable = false)
private char gender;
@Column(name = "email", nullable = true)
private String email;
public User() {}
public User(String username, String password, String firstName, String lastName, char gender, String email) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.email = email;
}
}
角色
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Builder
@AllArgsConstructor
public class Role {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "role_id", nullable = false)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "description", nullable = false)
private String description;
public Role () {}
public Role (String name, String description) {
this.name = name;
this.description = description;
}
}
的UserRole
import lombok.AllArgsConstructor;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@AllArgsConstructor
public class UserRole {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "user_role_id")
private Long id;
@OneToOne
@JoinColumn(name = "user_id")
private User user;
@OneToOne
@JoinColumn(name = "role_id")
private Role role;
public UserRole () {}
public UserRole (User user, Role role) {
this.user = user;
this.role = role;
}
}
运行应用程序时,会在数据库中正确创建Role
和UserRole
表,但{table}}表不是这种情况。我将非常感谢你的帮助:)。
答案 0 :(得分:0)
user
是postgresql中的保留关键字,因为它不允许使用此名称创建表。尝试使用其他名称。