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 860729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:51:38+00:00 2026-05-15T08:51:38+00:00

My database has two tables, question and field. Questions may have many fields, and

  • 0

My database has two tables, “question” and “field”. Questions may have many fields, and fields may have many fields. It’s a tree with a special root node.

I want to use them with hibernate (currently potgresql) – so it should be straightforward and simple to use it from java.

What is the best solution to this?

  1. add question_parent_id and field_parent_id to “field” table, and only use question_parent_id if it is a direct descendant of it. (check XOR which SQL constraint… may depend on SQL server)
  2. add question_parent_id and field_parent_id, and always use question_parent_id. Remember to stay consistent… (question_id should not change, probably not a real risk)
  3. Use the postgresql specific table inheritance: “question” and “field” extends “content”, so one foreign key column is enough. Use additional constraint on both “question” and “field”.
  4. use a third table (called “container”), consisting of just an id. Containers may have many fields, and a field may have one container. Questions have exaclty one container. But this requires additional code in java, and has a risk of infinite loops, even with an unique key on field_container_id…
  • 1 1 Answer
  • 1 View
  • 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-15T08:51:39+00:00Added an answer on May 15, 2026 at 8:51 am

    I would rather think about the class model than about the relational model. The user at the end doesn’t (usually) care how many keys you have in your database. He is using your classes, where it should be “straightforward and simple to use”. So write your class model first and think about mapping later.

    The solution in the database depends on your class model.

    Edit: Your model on the other side depends on what you need to do.

    Navigation: Do you usually need all fields from a question? Do you usually need only the fields directly assigned to a question or field or all field recursively down the tree? Do you need to know the parent of a field? etc. etc.

    Queries: Do you need to filter questions or fields by fields assigned to them? Recursively? Do you need to filter fields by parent? etc.

    In other words: You can’t optimize for everything. There are typical queries and typical navigation paths. Supporting too many ways could become expensive and could require redundant data in both the model and the database, which makes it hard to maintain.

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

Sidebar

Related Questions

I have a database framework where I have two tables. The first table has
I have a database that has two tables, these tables look like this codes
Excuse the long question! We have two database tables, e.g. Car and Wheel. They
I have two tables question and answers. - questions{id,user_id,question,date} - answers {id,q_id,user_id,answer, date} I
Well, I have two tables in an sql database. One has an ID column
I'm working on a SQL Server 2008 database. This database has two tables. Book
One of my tables in my SQL database has a growth rate of two
I have a Play Framework (1.2.4) project with PlayMorphia that has two different database
I have two grails servers: Server - has read/write access to the database Web
I have two tables in my database: Company table (ID, CompanyName, CompanyUsername, CompanyPassword) Employee

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.