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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:50:35+00:00 2026-06-06T11:50:35+00:00

Here is my code @Entity class Parent extends Person { @OneToMany(cascade = CascadeType.ALL, fetch

  • 0

Here is my code

@Entity
class Parent extends Person {
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "father")
    private List<Child> children;

    // ...

    public void addChild(Child c) {
        children.add(c);
    }

}

@Entity
class Child extends Person {
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "id")
    private Parent father;

    public Child() {
        this(NoParent.getInstance());//
    }

    public Child(Parent p) {
        super();
        setParent(p);
    }

    // ...
}

@MappedSuperclass
class Person {
    @Id
    private Integer id;
    private String name;
}

class MyParentService {

    public void addChild(String name, Parent parent) {
        Child c = new Child(parent);
        c.setName(name);
        parent.addChild(c);

        em.getTransaction.begin();
        em.merge(parent);
        em.getTransaction.commit();// Here two children with same name but different ids are created ...
    }
}    

Each time I run it, two children are added to the database while I just want one !
What am I doing wrong ?

Java 6
JPA 2
Hibernate 3.6.8.GA

  • 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-06T11:50:37+00:00Added an answer on June 6, 2026 at 11:50 am

    I read the Hibernate documentation which is really confusing and didn’t help me much.

    Notice, I have found that ObjectDB had recently faced AND fixed the exact same bug.
    (Hibernate guys why are you waiting for so long ??)

    Nevertheless, here the solution I came up with :

    STEP 1:
    Create a fresh new child (ie in detached state) and call its setParent method to link to its parent.

    STEP 2:
    Persist this child.

    STEP 3:
    Make changes to child as you need and merge it later.

    The solution is quite no elegant, but it works !
    Next you’ll fetch your parent object, it will have the previously child(ren) linked to it manually.

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

Sidebar

Related Questions

Here is the BlogPost model : @Entity @Table(name = blog_posts) public class BlogPost extends
I am using Entity Framework 4.1 code first . Here is my Category class:
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is code example class A{ int i; public: A(int i) : i(i) {}
enter code here Hi All, I have a simple windows service application that connects
Here is my code: for (DrawableEntity entity : drawableEntityList) { if (entity instanceof Beam)
When using the CTP 5 of Entity Framework code-first library (as announced here )
Please let me just show the code, @Entity class R { @Id Long id;
I've got a class with Parent and Children properties. ADO.NET Entity Framework Hierarchical Page
I've got a class with Parent and Children properties. ADO.NET Entity Framework Hierarchical Page

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.