简体   繁体   中英

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. I am learning to program in an elegant way in Java so please, excuse my ignorance if so.

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.

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? And an ArrayList of Document as attribute in 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?

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; I am new in Java so I am just using the basic Java (No ORM, No framework) . I don't know what Hibernate is, I am investigating.

Do I need to add an ArrayList of Category as an attribute of Class Document? And an ArrayList of Document as attribute in 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?

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:

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.

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.

In addition I'd like to mention that there are many frameworks available which support mapping data base content to Java objects. 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. That would be the purer OOP approach. Not sure why you marked this as 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 . 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.

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