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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:00:25+00:00 2026-06-12T17:00:25+00:00

I’m new both to spring and hibernate so take that into consideration in your

  • 0

I’m new both to spring and hibernate so take that into consideration in your answer.

I have a many-to-many relationship were the link-table must also contain information. I will try to solve this according to:

How Do I Create Many to Many Hibernate Mapping for Additional Property from the Join Table?

My Question is related to:

hibernate: Custom code on insert / update

But I just repeat it here so no clicking on link is required:

Lets say we have colors. A user can create a Mixture of n colors and store that mixture in the database. Later if a user searches for color “blue” all Mixtures containing blue should be displayed.

Business rule is that any color should only be in the database once. So if a new mixture is inserted, it must first be checked if all the colors are already in the database and if yes that color should be reused (referenced) and if not a new one should be created.

If a mixture is changed say “blue” is replaced with “red”, the behavior must be that the initial “blue” remains untouched and the system checks if “red” exists and either reuses it or creates it and then adds it to the mixture.

The important part is that “Colors” are managed by the system and an existing color must never change.”red” will always be “red” and should never be changed to “blue”. Since I’m new to Hibernate and Spring I’m kind of lost on how to implement this rule and at what level.IMHO I would put that logic at the lowest level possible so that it doesn’t break if you (the developer) forgets to check it. does that make sense? Better ideas or suggestions?

  • 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-12T17:00:27+00:00Added an answer on June 12, 2026 at 5:00 pm

    So here my current solution:

    1. Unique constraint in color
    2. In DAO update method check if the color to update already exists in database. if yes use it, if no create a new one
    3. Point 2 has an issue if the change to the color and the update happens in the same hibernate session. Then it will update that color from red to blue as the query done in 2. will always return the Color we are updating! I solved this by adding @Immutable to the Color entity class.

    Code for 2.

    public Color update(final Color color) {
        SQLQuery query = this.getCurrentSession().createSQLQuery(
            "select * from colors where color = :color")
            .addEntity(Color.class);        
        query.setString("color", color.getColor());
        Color result = (Color) query.uniqueResult();
        if (result == null) {
            Color newColor = 
                    new Color(color.getColor());
            this.getCurrentSession().persist(newColor);
            return newColor;
        } else {
            return (Color) this.getCurrentSession().merge(result);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't

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.