Entities in JPA

1

I am learning to use the Spring Boot framework and I need help to perform entity mapping.

This is the Query

  CREATE TABLE testdb.user_roles (
  user_role_id int(11) NOT NULL AUTO_INCREMENT,
  username varchar(20) NOT NULL,
  role varchar(20) NOT NULL,
  PRIMARY KEY (user_role_id),
  UNIQUE KEY uni_username_role (role,username),
  KEY fk_username_idx (username),
  CONSTRAINT fk_username FOREIGN KEY (username) REFERENCES testdb.users (username));


  CREATE  TABLE testdb.users (
  username VARCHAR(20) NOT NULL ,
  password VARCHAR(20) NOT NULL ,
  enabled TINYINT NOT NULL DEFAULT 1 ,
  PRIMARY KEY (username));

However, I need to do something like the following:

    Entity
    @Table(name = "roles")
    public class Roles {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="username")
    @Size(max = 45)
    @NotNull
    private String username;

    @Column(name = "role")
    @Size(max = 10)
    @NotNull
    private String role;
}

I appreciate any comments, ideas or solutions I'm looking for.

    
asked by Emanuel Cortez 13.04.2017 в 10:05
source

1 answer

0

Well, friend, you already have it; however, I'll give you a little help. We're just going to need @Entity , @Table , @Column , @Id , @Size , @Null and @NotNull .

We will make the User table and you will do the other:

CREATE  TABLE testdb.users (
  username VARCHAR(20) NOT NULL ,
  password VARCHAR(20) NOT NULL ,
  enabled TINYINT NOT NULL DEFAULT 1 ,
  PRIMARY KEY (username));



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

    @Id
    @Column(name="username")
    @Size(max = 20)
    private String username;

    @Column(name="password")
    @Size(max = 20)
    @NotNull
    private String password;

    @Column(name = "enabled")
    @NotNull
    private Integer enable;
}

That would be your class, only the getters and setters would be missing. I hope it helps you.

    
answered by 08.07.2017 в 23:38