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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:05:16+00:00 2026-05-13T13:05:16+00:00

I have these entities class Foo{ Set<Bar> bars; } class Bar{ Foo parent; String

  • 0

I have these entities

class Foo{
    Set<Bar> bars;
}

class Bar{
    Foo parent;
    String localIdentifier;
}

With this mapping (sorry, no annotations, I’m old fashioned):

<class name="Foo">
    ...
    <set name="bars" cascade="all-delete-orphan" lazy="false" inverse="true">
        <key>...</key>
        <one-to-many class="Bar"/>
    </set>
</class>


<class name="Bar">
    ...
    <property name="localIdentifier" column="local_identifier"/>
    <many-to-one name="parent" column="parent_id" />
</class>

I also have a unique constraint on 2 columns: local_identifier and parent_id (not a unique constrain on each, but a single unique constrain containing both, e.g. no 2 rows with the same parent and same localIdentifier are allowed)

alter table bar add constraint unique_bar unique (parent_id, local_identifier)

And this code that uses them:

//foo is persistent, foo id = 1
Bars bars = foo.getBars();
bars.clear(); // bars contained 1 item [parent_id = 1, local_identifier = "a"]
Bar newBar = new Bar();
newBar.setParent(foo);
newBar.setLocalIdentifier("a");
bars.add(newBar);

Now, for some reason, Hibernate doesn’t execute things in the order they were called. It doesn’t execute the clear() (delete) before the add() (insert) but vice versa, it first tries to insert, getting a ConstraintViolationException

I know adding a little session.flush() after bars.clear(); , could fix this, but in this case, I have no access to the session in a non ugly way.

So is flush is the only solution? or is there a Hibernate version that respects the order of actions?

Update:
By the way, dereferencing the collection will result in a HibernateException from https://www.hibernate.org/117.html#A3:

I get HibernateException: Don’t
dereference a collection with
cascade=”all-delete-orphan”
This
will happen if you load an object with
a cascade=”all-delete-orphan”
collection and then remove the
reference to the collection. Don’t
replace this collection, use clear()
so the orphan-deletion algorithm can
detect your change.

  • 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-13T13:05:16+00:00Added an answer on May 13, 2026 at 1:05 pm

    I guess there is no alternative to flushing

    From here:

    Hibernate is violating a unique constraint!

    Hibernate isn’t quite as
    clever with unique constraints as it
    is with foreign keys. Sometimes you
    might need to give a little hint.

    A unique constraint violation could
    occur if two objects are both being
    updated, one is “releasing” a value
    and the other is “obtaining” the same
    value. A workaround is to flush() the
    session manually after updating the
    first object and before updating the
    second.

    (This kind of problem occurs rarely in
    practice.)

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

Sidebar

Ask A Question

Stats

  • Questions 404k
  • Answers 404k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have not read/found any known issue regarding viewing WPF… May 15, 2026 at 5:22 am
  • Editorial Team
    Editorial Team added an answer The problem is that when I create more than one… May 15, 2026 at 5:22 am
  • Editorial Team
    Editorial Team added an answer What you are talking about is called minification. There are… May 15, 2026 at 5:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.