【问题标题】:creating relationship table at startup MySQL Spring Boot from data.sql在启动 MySQL Spring Boot 时从 data.sql 创建关系表
【发布时间】:2019-05-04 00:05:24
【问题描述】:

您好,我的任务是使用 Spring Boot 和 Hibernate 构建应用程序。问题是我正在通过 data.sql 文件构建数据库以保存数据并创建数据库。现在我需要在我的两个表之间创建一个关系表。这应该是一对多的关系,因为工作人员可以有一个用户帐户,也可以没有一个帐户。问题在于如何在应用程序启动时构建此关系表,因为引用应该是外键 user_idworker_id 我在启动时没有,因为它们是创建的通过休眠自动进行,因此无法对它们进行硬编码。所以问题是我将如何从 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;

我必须这样做:

1: 创建另一个实体,以便能够在数据库中创建一个新表来保存用户和工作实体的外键?

2:能否用data.sql中的脚本来完成。

我会在 users 表中创建一个引用 worker id 的列,但任务明确表示:

"创建第三个表来保存关系"

提前致谢

【问题讨论】:

    标签: spring hibernate jpa spring-data-jpa


    【解决方案1】:

    在 OneToMany 关系中,您可以拥有关于多方实体的所有需要​​的信息。创建另一个连接表只对多对多关系有意义,它只会在 OneToMany 上添加冗余数据并违反normalization

    话虽如此,如果你真的需要另一个表来定义两个实体的关系,你可以在不使用@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;
    }
    

    它将使用这种结构创建新的worker_user

    |WORKER_ID  |USER_ID  |
    

    【讨论】:

    • 我得到一个异常>非法尝试将非集合映射为'@OneToMany、@ManyToMany 或@CollectionOfElements' 我不知道为什么会出现此错误,因为我正在使用接口。我尝试使用 List,它也会抛出同样的错误。
    • 通过将单对多的变量更改为集合来解决问题。我将变量作为 Worker 对象。愚蠢的错误。感谢您的帮助
    • 我的错,我放错了 @OneToMany@ManyToOne 注释。已编辑,很高兴为您提供帮助!
    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2019-10-19
    • 2021-01-13
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多