John P
John P

Reputation: 1221

Spring create DB repository for SQL table

I have 2 tables in MySql and I have mapped them using hibernate in Spring: users and roles .

I have created one more table: user_roles but I don't know how to map it in hibernate.

You can see the table structure below:

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

Here is the mapping for the roles tables:

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

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

    private String name;
}

Here is the mapping for the users table:

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

...

}

I have created the POJO for the user_role table, but I don't know how to use hibernate on it, I am using @EmbeddedId annotation but it is not working. I don't know how to show the 2 classes above that they are embeddable

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

}

How can I map the "UserRole" class to 'user_role' in hibernate? Thank you!

Upvotes: 0

Views: 223

Answers (1)

Omid P
Omid P

Reputation: 211

There are two different ways which you can map user_roles table, which I suggest the first one :

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


}

Then create the entity.

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

}

Upvotes: 1

Related Questions