Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:20:07+00:00 2026-05-30T13:20:07+00:00

I will try to explain my problem clearly: I have two tables: document and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:20:09+00:00Added an answer on May 30, 2026 at 1:20 pm

    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 (e.g. 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 (e.g. 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 (e.g. hibernate).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem and I will try to explain the issue: I have
I will try to explain my problem. I have 4 files index.html, event.js, start.js
I will try to explain the problem that I have with this code. This
I have CSS problems. I will try to explain my problem to you with
i will try to explain the problem. I have this structure: offers -------------- id_offer|offer|company
I will try to explain what exactly I need to do. I have 3
Good day, i will try to explain my problem as best as i can.
I am having difficulty to explain this problem, but I will try anyway. I
I am not sure whether the title clearly explains my problem, will try to
I will try and explain my problem as accurately as possible, I don't think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.