春季为SQL表创建数据库存储库

时间:2018-11-03 21:27:19

标签: java sql hibernate

我在MySql中有2个表,并在Spring中使用hibernate映射了它们: users roles

我还创建了一个表: user_roles ,但是我不知道如何在休眠状态下映射它。

您可以看到以下表格结构:

CREATE TABLE users (
    username varchar(30) NOT NULL,
    email varchar(50) NOT NULL,
    password varchar(255) NOT NULL,
    first_name varchar(40) NOT NULL,
    last_name varchar(40) NOT NULL,
    date_of_birth Date,
    phone_number varchar(20),

    PRIMARY KEY (username)
);

CREATE TABLE roles (
    role_id INTEGER NOT NULL AUTO_INCREMENT,
    name VARCHAR(255),
    PRIMARY KEY (role_id)
);

CREATE TABLE user_roles (
    username VARCHAR(30) NOT NULL,
    role_id INTEGER NOT NULL,
    PRIMARY KEY (username, role_id)
);

这是角色表的映射:

@Entity
@Table(name = "roles")
public class Role implements Serializable {

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

    private String name;
}

这是用户表的映射:

@Entity
@Table(name = "users")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements Serializable {

@Id
@NotEmpty
@Size(min = 5, max = 15)
@Column(name = "username")
private String username;

@Email
@NotEmpty
@Column(name = "email")
private String email;

@NotEmpty
@Size(min = 5)
@Column(name = "password")
private String password;

@NotEmpty
@Size(max = 40)
@Column(name = "first_name")
private String firstName;

@NotEmpty
@Size(max = 40)
@Column(name = "last_name")
private String lastName;

...

}

我已经为 user_role 表创建了POJO,但是我不知道如何在其上使用休眠,我正在使用 @EmbeddedId 批注,但不是工作。我不知道如何显示以上两个可嵌入的类

@Entity
@Table(name = "user_roles")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserRole implements Serializable {

public UserRole() { }

public UserRole(User username, Role role_id) {
    this.username = username;
    this.role_id = role_id;
}

private static final long serialVersionUID = -2947211066236048069L;

@EmbeddedId
private User username;

@EmbeddedId
private Role role_id;

}

如何在休眠状态下将“ UserRole”类映射到“ user_role”?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以通过两种不同的方式映射user_roles表,我建议使用第一种方式:

1。

@Entity
@Table(name = "users")
public class User 
{



    @ManyToMany(targetEntity = Role.class)
    @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    @NotAudited
    private Set<Role> roles = new HashSet<>();





}

2。

@Embeddable
public class UserRoleId implements java.io.Serializable
{

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

    @Column(name = "role_id", nullable = false)
    private long roleId;

    public UserRoleId()
    {
    }

    public UserRoleId(long userId, long roleId)
    {
        this.userId = userId;
        this.roleId = roleId;
    }


    //hashcode equal


}

然后创建实体。

@Entity
@Table(name = "users_roles")
public class Userroles implements java.io.Serializable
{

    @EmbeddedId
    private UserRoleId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
    @NotNull
    private Users users;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "role_id", nullable = false, insertable = false, updatable = false)
    @NotNull
    private Role role;

}