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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:25:51+00:00 2026-05-27T15:25:51+00:00

Update: I rewrote the question to make as much sense as possible and provide

  • 0

Update: I rewrote the question to make as much sense as possible and provide the most information.

I have the following:

@Entity
public class FooLnkBar implements java.io.Serializable {
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "SomeID", nullable = false)
    private Foo foo;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OtherID", nullable = false)
    private Bar bar;
}

@Entity
public class Foo implements java.io.Serializable {
    @OneToMany(orphanRemoval=true, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "foo")
    private Set<FooLnkBar> fooLnkBars = new HashSet<FooLnkBar>(0);
}

Case 1: Save everything by persisting FooLnkBar

 // This correctly saves everything into the database in one shot
 Foo foo = new Foo();
 Bar bar = new Bar();
 FooLnkBar flb = new FooLnkBar();
 flb.setFoo(foo);
 flb.setBar(bar);
 persist(flb);

Case 2: Modify a FooLnkBar and persist a Foo

 // Load a Foo from the database and get the set of FooLnkBars
 Foo foo = loadFooFromDatabase();
 Set<FooLnkBar> fooLnkBars = foo.getFooLnkBars();

 // Delete all existing FooLnkBar objects. Alternatively use removeAll()
 Iterator<FooLnkBar> it = fooLnkBars.iterator();
 while (it.hasNext()) {
   FooLnkBar o = it.next();
   it.remove();
 }

 // Add some new FooLnkBars
 Foo foo = new Foo();
 Bar bar = new Bar();
 FooLnkBar flb = new FooLnkBar();
 fooLnkBars.add(flb);
 foo.setFooLnkBars(fooLnkBars);

 // Save all changes
 persist(foo);

Case 2 does not work. Because of the cascadeType, this also removes the Foo in which the FooLnkBar resides, so the entire Foo object in Case 2 is deleted. This is bad.

Furthermore, I want to ensure that hibernate doesn’t see Foo as a duplicate row when I try to persist it since FooLnkBar is a value in another table.

If I delete a Foo from the database the FooLnkBar should also be deleted from the database.

If I delete a FooLnkBar from the database the Foo should NOT be deleted from the database.

I am looking for a way to get all cases working.

Update: Changing this fixes Case 2 but breaks Case 1:

   @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
   @JoinColumn(name = "SomeID", nullable = false)
   private Foo foo;

The Error message in Case 1 is now “not-null property references a null or transient value: FooLnkBar.foo”

  • 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-27T15:25:52+00:00Added an answer on May 27, 2026 at 3:25 pm

    It seems to be doing exactly what you told it to:

    ObjectOne obj1 = findInDatabase();

    Load a persistent object.

    obj1.addSet(objTwoSet);

    Add the content of a set to the object’s existing set (thus “merging” the two sets).

    sessionFactory.getCurrentSession().saveOrUpdate(obj1);

    Unnecessarily saveOrUpdate() the object. The object is already attached to the session, and therefore its state will properly be persisted when the enclosing transaction commits. You can do away with this line.

    In order to clear the existing content of the set and add all new content, you should do exactly that:

    obj1.clearSet();
    obj1.addSet(objTwoSet);
    

    Update: A Set isn’t treated as a single, discrete object in Hibernate. It’s just a grouping of entities, so when you say “remove the first Set from the database”, you’re actually saying one of “remove all the entities contained in the first set from the database” or “disassociate all the entities contained in the first set from the given object in the database”.

    For the former, you’d use the orphanRemoval attribute of @OneToMany, and see the prototypical parent/child relationships demonstrated by the reference guide.

    For the latter, you just do exactly what you’re doing, but if it’s a bi-directional relationship, you must also correctly set the other end. So instead of a simple:

    obj1.addSet(objTwoSet);
    

    you need something like:

    for (ObjectTwo o : obj1.getSet()) {
        o.setObjectOne(null);
    }
    obj1.addSet(objTwoSet);
    

    This is most likely your problem. It’s never okay to leave your object model in a broken state.

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

Sidebar

Related Questions

Completely rewrote the question as I now have more information about what is happening.
UPDATE (2010-12-21): Completely rewrote this question based on tests that I've been doing. Also,
I'll like to update the following rewrite condition to only allow one particular subdomain.
UPDATE I tried using the internal wordpress rewrite. What I have to do is
UPDATE 6/21/12 I have a form in rails that is working similar to an
I have the following: <ul style=width: 300px; list-style-type:none> <li> <table style=width:100% border=0 cellpadding=0 cellspacing=0
EDIT: Rewrote the question. Added bounty as its important for me. The final hint
I have been writing an assembly to simplify much of my POCO type generation.
I have an application where Servlet has a method called Update(ReqIn, ReqOut) . I
I have the following .htaccess at the moment. This is a dummy copy paste

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.