简体   繁体   中英

creating relationship table at startup MySQL Spring Boot from data.sql

Hello i have a task to build an application using spring boot and hibernate. The thing is i'm building the database through a data.sql file to persist data and create the database. Now i need to create a relationship table between my two tables. This should be a one to many relationship given that a worker can have a user account or may not have an account. The problem lies on how to build this relationship table when the application starts because the references should be the foreign keys user_id and worker_id which i do not have at startup because they are created automatically by hibernate so there's no way to hardcode them. So the question is how would i go about creating this relationchip table from data.sql.

data.sql:

INSERT INTO worker (name, last_name, status) VALUES
('manuel', 'dias', 'active'),
('sailin ', 'nal', 'active'),
('abraham', 'lincon', 'active'),
('mick', 'smith', 'active'),
('jose', 'perez', 'inactive'),
('luis', 'nuñez', 'inactive'),
('ruben', 'puertas', 'inactive'),
('anders', 'stone', 'inactive'),
('luis', 'alvarez', 'deleted'),
('claudio', 'martinez', 'deleted'),
('morfeo', 'rodriguez', 'active'),
('onetys', 'estrada', 'inactive'),
('rene', 'fajardo', 'active');

INSERT INTO users (username, password, user_type, status) VALUES
('madi', 'madi', 'worker', 'active'),
('sana', 'sana', 'worker', 'active'),
('abli', 'abli', 'worker', 'active'),
('mism', 'mism', 'worker', 'active'),
('jope', 'jope', 'worker', 'active'),
('lunu', 'lunu', 'worker', 'active'),
('rupu', 'rupu', 'worker', 'active'),
('anst', 'anst', 'worker', 'active'),
('lual', 'lual', 'worker', 'active'),
('clma', 'clma', 'worker', 'active');

Users.class:

@Entity
@Table(name = "users")
public class Users implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id", nullable = false)
    private int userId;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;
    @Column(name = "userType")
    private String userType;
    @Column(name = "status")
    private String status;


    @ManyToOne
    @JoinColumn(name = "worker_id")
    private Worker worker;

Worker.class:

@Entity
@Table(name = "worker")
public class Worker implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "worker_id")
    private int workerId;
    @Column(name = "name")
    private String name;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "status")
    private String status;

    @OneToMany(mappedBy = "worker", cascade = CascadeType.ALL)
    private Collection<Users> users;

So do i have to:

1: Create another Entity to be able to make a new table in the database holding the foreign keys to user and worker entities?

2: Can it be done with a script in data.sql.

I would create a column in users table referencing the worker id but the task explicitly says:

"make a third table to hold the relationship"

Thanks in advance

In OneToMany relationship, you can have all needed information on the entity of the many side. Creating another join table only makes sense on ManyToMany relationship, it would only add redundant data on OneToMany and violates normalization .

That being said, if you really need another table to define the two entities relationship, you can achieve this without creating another entity using @JoinTable .

@Entity
@Table(name = "users")
public class Users implements Serializable {
    //....
    @ManyToOne
    @JoinTable(name = "worker_users",
                joinColumns =  @JoinColumn(name = "user_id"),
                inverseJoinColumns = @JoinColumn(name = "worker_id"))
    private Worker worker;
}


@Entity
@Table(name = "worker")
public class Worker implements Serializable {
    //...
    @OneToMany(cascade = CascadeType.ALL) //remove mappedBy
    @JoinTable(name = "worker_users",
                joinColumns =  @JoinColumn(name = "worker_id"),
                inverseJoinColumns = @JoinColumn(name = "user_id"))
    private Collection<Users> users;
}

It would create new worker_user table with this structure

|WORKER_ID  |USER_ID  |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM