简体   繁体   English

如何从具有多对多关系的MySQL数据库中检索对象?

[英]How to retrieve objects from a MySQL Database with a many-to-many relationship?

I have 2 tables: Document and Category with a many to many relationships. 我有2个表:具有多对多关系的文档类别 In addition, the relationship has an attribute called weight . 另外,该关系具有称为weight的属性。 I also have the corresponding Classes with Document being represented like this: 我也有与Document相对应的类,表示方式如下:

public class Document {
    private Integer id; // Id from DB, 0 if new object
    private String name;
    private String url;
    private Date dateCreated;
    private List<Category> categories;

    // getters & setters...
    }

Now I have 2 questions - best practices concerns especially. 现在,我有2个问题-特别是有关最佳做法的问题。 I want to retrieve data from the database (MySQL) and and build my objects. 我想从数据库(MySQL)检索数据并构建对象。 I use a SELECT statement like that: 我使用这样的SELECT语句:

SELECT doc.*, GROUP_CONCAT(cat.CategoryID SEPARATOR ',') AS CategoryGrp FROM document doc
LEFT JOIN doc_category dc ON doc.Doc_ID = dc.DC_DocID
LEFT JOIN category cat ON dc.DC_CategoryID = cat.CategoryID
GROUP BY doc.Doc_ID

In the resultset, rs.getString(5) --> CategoryGrp will have the IDs of the related categories. 在结果集中, rs.getString(5)-> CategoryGrp将具有相关类别的ID。 Now how do I proceed to build my object Document by linking its Categories as the Class requires it. 现在,如何按照类的要求通过链接其类别来继续构建对象文档。

I tend to do new SELECT request on table Category to get the others details (category name for instance) in a loop while still on the first ResultSet. 我倾向于对表Category进行新的SELECT请求,以在仍然在第一个ResultSet上的情况下循环获取其他详细信息(例如,类别名称)。 Is that a good practice? 这是个好习惯吗? Isn't there a "recommended" way? 有没有“推荐”的方式?

My second question is about the attribute weight in junction table. 我的第二个问题是关于联结表中的属性权重 In which object should I record its value? 我应该在哪个对象中记录其值? I guess it's Category but not very sure since Category object can't have a weight on its own... 我猜它是Category,但不是很确定,因为Category对象本身不能具有权重 ...

Hope I have clear enough. 希望我已经足够清楚了。 Thanks 谢谢

Answer to 1st question: 2 ways: one sql or like you suggest 2 sql. 回答第一个问题:2种方法:一个sql或像您建议的2 sql。 (retriev the docs and for every cat id perform sql to retrive cat attributes) I think this is better: Get your all docs with different cats and its attributes. (检索文档,并为每个cat id执行sql来检索cat属性)我认为这更好:将所有具有不同cats及其属性的文档都获取。 Loop it and compare the doc with the previous one. 循环播放,然后将文档与上一个文档进行比较。 If different, then you have a new doc object. 如果不同,则您有一个新的doc对象。 Every line in the loop is a different cat object. 循环中的每一行都是一个不同的cat对象。

doc1 cat1
doc1 cat2
doc1 cat3
doc2 cat4
doc3 cat5
doc3 cat6
... 

Answer to 2nd question: I think you should consider this: is the weight more related to doc or cat? 对第二个问题的答案:我认为您应该考虑以下问题:重量与doc或cat更相关吗? On the other hand, you cannot have multiple weights on 1 doc, so my guess would be to add the to cat (as that is a 1 on 1 relationship. 另一方面,您不能对1个文档具有多个权重,所以我的猜测是将cat添加到cat(因为这是一对一的关系。

Many-to-many relationship in RDBMS are ALWAYS done by at least 3 tables. RDBMS中的多对多关系总是至少由3个表完成。 So you need a third table, which will contain the ID`s from the first two, all of your SELECT-s will go through this third table. 因此,您需要第三个表,其中将包含前两个表的ID,所有SELECT都将通过该第三个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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