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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:21:17+00:00 2026-06-11T04:21:17+00:00

I have 2 tables, Word and State, State contains 2 cols, ID and CurrentState,

  • 0

I have 2 tables, Word and State,
State contains 2 cols, ID and CurrentState, it’s 3 static rows are 1-Active, 2-InActive, 3-Other
Word is the table I am adding rows to. It has 4 cols, ID, Value, Description and CurrentState.
It has a foreign key to State on the column currentState
Here is the working code I have that creates a Word, sets it’s currentState field and persists it.

Word word = new Word(); 
word.setValue("someWord");
word.setDescription("some description for this word");      
State state = new State(1,"Active");
word.setState(state);   
worddao.saveOrUpdate(word);

The thing is this just doesn’t look right. What is the best practice for creating the State instance so that I can create a Word which points to a valid State row. Is Enumeration an option here? I mean I could accidently create a State with ID = 5 and violate the foreign key constraint. I want to prevent this from happening in the first place. Any ideas?

  • 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-06-11T04:21:18+00:00Added an answer on June 11, 2026 at 4:21 am

    The safe way to do this is to look up the state object instead of creating a new one. You can mark the entity as cacheable and also mark the query to retrieve State as cacheable to avoid unnecessary queries.

    Word word = new Word(); 
    word.setValue("someWord");
    word.setDescription("some description for this word");      
    State state = stateDao.findByState("Active")
    word.setState(state);   
    worddao.saveOrUpdate(word);
    

    The other option (which I generally follow) for master/reference data table is to not have an numeric ID. Just have one column which is the String representation of it and use that as the foreign key column.

    The advantage with this is that when you are looking at data in the database – the word table the state will be easy to interpret. You don’t have to do one more mental lookup to understand the data.

    The other advantage is that caching becomes easier. We just need to cache the entity and not the query, since the lookup is always by primary key.

    Later on if required you can add another column which is a display label.

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

Sidebar

Related Questions

Problem: I have about 50,000 rows in Excel. Each row contains a the word
I have a table of 2 fields. Word and timestamp. Then i have this
I have an online dictionary. All inputs are saved in table id-word-unixtime. Now I
I have tables linked by FK, I query on the first table using entity
I have two tables in my database, 1st one contains a couple of sentences,
I have 3 tables Person, Names, and Notes. Each person has multiple name and
I have a report that contains a table of data this data is set
i have 2 tables for categories & posts the categories table contain id name
I have two tables: Dictionary, Names. Both tables have large number of rows (like
i have two tables in my database one is A other one is B

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.