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

  • Home
  • SEARCH
  • 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 7563865
In Process

The Archive Base Latest Questions

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

I have 2 tables: Document and Category with a many to many relationships. In

  • 0

I have 2 tables: Document and Category with a many to many relationships. In addition, the relationship has an attribute called weight.
I also have the corresponding Classes with Document being represented like this:

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.
I want to retrieve data from the database (MySQL) and and build my objects. I use a SELECT statement like that:

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. 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.
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…

Hope I have clear enough.
Thanks

  • 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:46:25+00:00Added an answer on May 30, 2026 at 1:46 pm

    Answer to 1st question:
    2 ways: one sql or like you suggest 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. Loop it and compare the doc with the previous one. If different, then you have a new doc object. Every line in the loop is a different cat object.

    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? 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.

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

Sidebar

Related Questions

We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
Imagine I have two tables. Document int DocumentId (PK) char DocumentName Attribute int DocumentId
I have two tables document (id, file) similarity(id1,id2,similarityvalue) where id1 and id2 are foreign
I have two tables: Agents with columns: agentid, name DocumentsRead with columns: agentid, document,
I have an HTML document where I have two different tables. One is class
I'm using tables in my document, and I want to be able to have
I will try to explain my problem clearly: I have two tables: document and
I have two fields... one is category name and other is document title... When
I have two tables: account_company and document_invoice. Table account_company has 2 columns: company_id and
I have 2 tables, a document table and a content table. They look like

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.