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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:59:28+00:00 2026-05-27T03:59:28+00:00

I have a Parent entity with a Child entity in a ManyToOne relationship: @Entity

  • 0

I have a Parent entity with a Child entity in a ManyToOne relationship:

@Entity class Parent {
  // ...
  @ManyToOne((cascade = {CascadeType.ALL})
  private Child child;
  // ...
}

The Child has an unique field:

@Entity class Child {
  // ...
  @Column(unique = true)
  private String name;
  // ...
}

When I need a new Child, I ask the ChildDAO first:

Child child = childDao.findByName(name);
if(child == null) {
  child = new Child(name);
}

Parent parent = new Parent();
parent.setChild(child);

The problem is, if I do like above twice (with the same name for the Child), and only persist the Parent at the end, I get a constraint exception. Which seems normal, since initially there was no child in the database with the specified name.

The problem is, I’m not sure what would be the best way to avoid this situation.

  • 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-27T03:59:29+00:00Added an answer on May 27, 2026 at 3:59 am

    You are creating two non-persistent instances of Child with new Child() twice then putting these in two different Parents. When you persist the Parent objects, each of the two new Child instances will be persisted/inserted via cascade, each with a different @Id. The unique constraint on the name then breaks. If you’re doing CascadeType.ALL, then every time you do new Child() you may be getting a separate persistent object.

    If you really wanted the two Child instances to be treated as a single persistent object with the same ID, you would need to persist it separately to associate with the persistence context/session. Subsequent calls to childDao.findByName would then flush the insert and return the new Child you just created, so you won’t be doing new Child() twice.

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

Sidebar

Related Questions

I have 2 entities in a to-many relationship, Parent-->Child. The Child entity has an
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
Say I have a unidirectional @ManyToOne relationship like the following: @Entity public class Parent
I use Entity Framework 4 and I have parent - child relation with Cascade
I have a parent child relationship between 2 classes Parent @Entity @Table(name = PARENT)
I have two entities types: RunContainer parent entity type Run child entity type Run
I have parent/child relationship set up via Node Reference. A Child record can have
I have a parent object which has a one to many relationship with an
I'm using Entity Framework 4 and have a one-to-many relationship between a parent and
I am using Entity Framework CodeFirst where I have used Parent Child relations using

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.