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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:31:08+00:00 2026-05-27T08:31:08+00:00

I have 2 parent classes A, C and 2 children: B, D. Both relationships

  • 0

I have 2 parent classes A, C and 2 children: B, D. Both relationships have transitive persistence enabled. However persisting D requires B to be persistent.

class A {
    @OneToMany(cascade = {CascadeType.PERSIST}, mappedBy = "a")
    public Set<B> getBs() {
     ...
    }
}

class B {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "a_id", nullable = false)
    public A getA() {
      ...
    }
}

class C {
    @OneToMany(cascade = {CascadeType.PERSIST}, mappedBy = "c")
    public Set<D> getDs() {
       ...
    }
}

class D {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "b_id", nullable = false)
    public B getB() {
      ...
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "c_id", nullable = false)
    public C getC() {
      ...
    }
}

What I am trying is to save both B and D in one shot:

A a = entityManager.getReference(...);
C c = entityManager.getReference(...);

B b = new B();

D d = new D();
d.setB(b);

a.add(b);
b.setA(a);

c.add(d);
d.setC(c);

entityManager.flush();

I assume this should be possible. However I get a TransientObjectException telling that B needs to be persistent when trying to save D.

Is there a specific order the cascading relations are processed by hibernate?

  • 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-27T08:31:09+00:00Added an answer on May 27, 2026 at 8:31 am

    When you flush, d is made persistent thanks to the cascade from C to D. But there is no cascade from D to B, so Hibernate complains. A would need to be flushed before D for it to work, but there’s no reason to have any specific order between A and D.

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

Sidebar

Related Questions

New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
In Django, when you have a parent class and multiple child classes that inherit
I have a MustInherit Parent class with two Child classes which Inherit from the
I've some classes, for example, Parent and Children. Parent have a collection of Children
If I have a parent class: public class Parent { public Parent() { Children
I have 2 Classes, Parent and Child, with a @OneToMany relationship. Every parent has
I have the following basic classes (cut down for this question): public class Parent
I have two classes: public class Parent { public virtual long? ID { get;
I have three classes with common parent. Let's say parent is Animal and children
Let's say that I have classes like this: public class Parent { public int

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.