Greetings to all!
I am trying to create an application using Spring and Postgres. Currently I have 3 entities which are: User
, Role
and UserRole
User
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;
}
}
Role
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;
}
}
At the time of running the project, both the tables Role
and UserRole
are created correctly in the database. However, the User
table can not be created. I've been thinking about it but I do not see why the error happens to me. I would appreciate it if you could help me.