【问题标题】:Connecting 3 tables with hibernate / spring-boot使用休眠/弹簧引导连接 3 个表
【发布时间】:2021-11-12 08:10:10
【问题描述】:

我无法理解如何使用 spring-boot/hibernate 连接 3 个表。 表格是:用户、技术、类别

每个用户都拥有这 10 个类别,但在这些类别中,他们可以保存一项或多项技术。每种技术都可以分为几个不同的类别。

我有一个代码现在部分工作,而不是引用表类别,我只是创建新类别,所以我的 BDD 中有重复项。 理想情况下,我希望每个用户数据结构都像这样(伪代码):

{
    {
    "category1" : {id, name}
    "technologies" [{id, name}, {id, name}, {id, name} ]
    },

    {
    "category2" : {id, name}
    "technologies

    }
    .
    .
    .   
}

我的桌子是:

用户表

public class MyUser {

// other properties

@OneToMany(mappedBy="id")
    private Collection<Category> categories;
}

技术

public class Technology {

// other properties

    @Id
    @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
    private int id;

    @Column(name = "name")
    private String name;
}

技术类别:

public class TechnologyCategory {

// other properties

    @Id
    @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
    private int id;

    @Column(name="name")
    private String name;
}

以及我试图将用户与类别联系起来的表格(每个类别都有技术列表)

USER_CATEGORIES

public class UserCategory {

    @Id
    @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
    private int id;

    @Column(name = "name")
    private String technologyCategory; // here I would love to reference technology category table

    @ManyToMany()
    Collection <Technology> technologies;
}

所以我已经尝试/阅读了这个: Joining three tables using MySQL

ManyToManyToMany - Joining three tables with Hibernate annotations

Hibernate: How to Join three 3 tables in one join table in Annotation?

Hibernate: mapping 3 tables

但没有成功,因为每次尝试实施上述解决方案都会导致我无法解决的异常(所有与休眠相关的无法创建表)。 谢谢

【问题讨论】:

    标签: java sql spring-boot hibernate datatables


    【解决方案1】:

    如果我理解正确,假设您的示例代码有效,这样的事情对您有用:

    用户类,加入类别:

    public class User {
    
        @Id
        @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
        private int id;
    
        // other properties
    
        @ManyToMany(fetch = FetchType.EAGER)
        @JoinTable(name = "user_categories", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"))
        private Set<Category> categories;
        
    }
    

    类别实体,包含技术的类别和技术:

    public class Category {
    
        @Id
        @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
        private int id;
    
        // other properties
    
        @ManyToOne
        @JoinColumn(name = "technology_category_id")
        private TechnologyCategory category;
        
        @ManyToMany(fetch = FetchType.EAGER)
        @JoinTable(name = "category_technologies", joinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "technology_id", referencedColumnName = "id"))
        private Set<Technology> technologies;
        
    }
    

    TechnologyCategory 实体:

    public class TechnologyCategory {
    
        // other properties
    
        @Id
        @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
        private int id;
    
        @Column(name="name")
        private String name;
    }
    

    技术实体:

    public class Technology {
    
        // other properties
    
        @Id
        @SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
        private int id;
    
        @Column(name="name")
        private String name;
    }
    

    如果您不希望 id 出现在 JSON 中,只需将 @JsonIgnore 注释放在 id 属性上方即可。

    【讨论】:

    • 非常感谢您的回答。我现在就试试,然后回来找你!
    猜你喜欢
    • 2015-03-11
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多