简体   繁体   English

您如何在Java中的两个元素之间创建关系?

[英]How do you create relationship between two elements in Java?

I will try to explain my problem clearly: I have two tables: document and category and they have a many-to-many relationship. 我将尝试清楚地解释我的问题:我有两个表:document和category,它们之间存在多对多关系。 I am learning to program in an elegant way in Java so please, excuse my ignorance if so. 我正在用Java优雅地学习编程,所以请原谅我的无知。

I am doing a search on my database and I want to illustrate the same relationship between the retrieved element (a document) and its categories, when I create the objects from the resultSet. 我正在数据库上进行搜索,当我从resultSet创建对象时,我想说明检索到的元素(文档)与其类别之间的相同关系。

I have created a Class Document and a Class Category; 我创建了一个班级文档和一个班级类别; Do I need to add an ArrayList of Category as an attribute of Class Document? 我是否需要添加Category的ArrayList作为Class Document的属性? And an ArrayList of Document as attribute in Category? 还有一个文档的ArrayList作为Category中的属性?

I also need to do modifications on some Documents, do I need to create an attribute id in Class Document so store the id from the table in DB so that the update is easier? 我还需要对某些文档进行修改,是否需要在类文档中创建属性ID,以便将表中的ID存储在DB中,以便更轻松地进行更新?

My Class Document might look like: 我的班级文档可能看起来像:

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

    // getters & setters...
}

This might be confusing but I just want to things in a "standard" way! 这可能会令人困惑,但我只想以“标准”方式处理问题! I know how to do all this in a "dirty" way by using multiple queries etc... but once again I need idea from experienced OO developpers! 我知道如何通过使用多个查询等方式以“肮脏的”方式完成所有这些工作……但是我再次需要经验丰富的面向对象开发人员的想法!

EDIT: I removed MVC; 编辑:我删除了MVC; I am new in Java so I am just using the basic Java (No ORM, No framework) . 我是Java的新手 ,所以我只是使用基本的Java (没有ORM,没有框架) I don't know what Hibernate is, I am investigating. 我正在调查Hibernate是什么,我不知道。

Do I need to add an ArrayList of Category as an attribute of Class Document? 我是否需要添加Category的ArrayList作为Class Document的属性? And an ArrayList of Document as attribute in Category? 还有一个文档的ArrayList作为Category中的属性?

It depends on the way you'd like to access your objects. 这取决于您要访问对象的方式。 If it is sufficient for your use cases to access categories via documents 如果您的用例足以通过文档访问类别

document1.getCategories().get(0)...

and you don't need to access documents via categories like this 而且您不需要通过这样的类别访问文档

category1.getDocuments().get(0)...

, then it will be sufficient to have a single list. ,那么只有一个列表就足够了。 In this case you'd call the relationship between those classes unidirectional. 在这种情况下,您可以将这些类之间的关系称为单向的。 If both ways are required you need two lists. 如果同时需要两种方式,则需要两个列表。 In this case your relation ship is bidirectional. 在这种情况下,您的关系船是双向的。 Anyway, if a unidirectional relationship is sufficient, you should prefer that one as you don't have to keep both lists in sync in that case. 无论如何,如果单向关系就足够了,则您最好选择一个关系,因为在这种情况下不必使两个列表保持同步。


I also need to do modifications on some Documents, do I need to create an attribute id in Class Document so store the id from the table in DB so that the update is easier? 我还需要对某些文档进行修改,是否需要在类文档中创建属性ID,以便将表中的ID存储在DB中,以便更轻松地进行更新?

This is some kind of design question. 这是某种设计问题。 In most cases it makes sense to include surrogate data base keys as it will make it easier to execute updates. 在大多数情况下,包含代理数据库密钥是有意义的,因为这将使执行更新更加容易。 Anyway, if your object can be identified by other attributes (eg the name) or a combination of those attributes it is of course not required to include the surrogate key. 无论如何,如果您的对象可以由其他属性(例如名称)或这些属性的组合来标识,则当然不需要包括代理键。


As you've said that you are learning Java I'd like to add some general remarks: 正如您所说的,您正在学习Java时,我想补充一些一般性说明:

You've declared the list in the example like this: 您已经在示例中声明了列表,如下所示:

private ArrayList<String> category;

You should prefer the interface List instead of the concrete implementation class ArrayList here. 您应该首选接口List而不是此处的具体实现类ArrayList。

private List<String> category = new ArrayList<String>();

This will allow you to use any kind of implementation (eg ArrayList or LinkedList) as your code does no longer depend on the concrete implementation class. 这将允许您使用任何类型的实现(例如ArrayList或LinkedList),因为您的代码不再依赖于具体的实现类。

In addition I'd like to mention that there are many frameworks available which support mapping data base content to Java objects. 另外,我想提及的是,有许多可用的框架支持将数据库内容映射到Java对象。 So if you have to do a lot of mappings you should definitely consider using one of them (eg hibernate). 因此,如果必须执行许多映射,则绝对应该考虑使用其中一种映射(例如,休眠)。

If you are using an ORM like Hibernate you would use an ArrayList<Category> instead of one for strings. 如果您使用的是Hibernate之类的ORM,则可以使用ArrayList<Category>代替字符串。 That would be the purer OOP approach. 那将是更纯正的OOP方法。 Not sure why you marked this as MVC. 不知道为什么将其标记为MVC。

Thomas's answer is very good. 托马斯的答案很好。 I just want to add that if you want a bidirectional relationship, you will need to be careful to ensure that the lists of partners on both sides are kept consistent. 我只想补充一点,如果您要建立双向关系,则需要小心以确保双方的合作伙伴列表保持一致。

There's a good article about this by Vincent Partington . 文森特·帕廷顿(Vincent Partington)对此发表一篇很好的文章 He is writing from the point of view of writing objects for JPA, but the pattern he describes (with the internal* methods) is perfectly applicable to plain objects. 他是从为JPA编写对象的角度来编写的,但是他描述的模式(使用internal*方法)完全适用于普通对象。

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

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