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

  • Home
  • SEARCH
  • 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 3235132
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:28:17+00:00 2026-05-17T17:28:17+00:00

I have a one-to-many relation between Parent and Child table. In the parent object

  • 0

I have a one-to-many relation between Parent and Child table. In the parent object I have a

List<Child> setChildren(List<Child> childs)

I also have a foreign key in the Child table. This foreign key is an ID that references a Parent row in database. So in my database configuration this foreign key can not be NULL.
Also this foreign key is the primary key in the Parent table.

So my question is how I can automatically save the children objects by doing something like this:

session.save(parent);

I tried the above but I’m getting a database error complaining that the foreign key field in the Child table can not be NULL. Is there a way to tell JPA to automatically set this foreign key into the Child object so it can automatically save children objects?

  • 1 1 Answer
  • 1 View
  • 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-17T17:28:18+00:00Added an answer on May 17, 2026 at 5:28 pm

    I tried the above but I’m getting a database error complaining that the foreign key field in the Child table can not be NULL. Is there a way to tell JPA to automatically set this foreign key into the Child object so it can automatically save children objects?

    Well, there are two things here.

    First, you need to cascade the save operation (but my understanding is that you are doing this or you wouldn’t get a FK constraint violation during inserts in the “child” table)

    Second, you probably have a bidirectional association and I think that you’re not setting “both sides of the link” correctly. You are supposed to do something like this:

    Parent parent = new Parent();
    ...
    Child c1 = new Child();
    ...
    c1.setParent(parent);
    
    List<Child> children = new ArrayList<Child>();
    children.add(c1);
    parent.setChildren(children);
    
    session.save(parent);
    

    A common pattern is to use link management methods:

    @Entity
    public class Parent {
        @Id private Long id;
    
        @OneToMany(mappedBy="parent")
        private List<Child> children = new ArrayList<Child>();
    
        ...
    
        protected void setChildren(List<Child> children) {
            this.children = children;
        }
    
        public void addToChildren(Child child) {
            child.setParent(this);
            this.children.add(child);
        }
    }
    

    And the code becomes:

    Parent parent = new Parent();
    ...
    Child c1 = new Child();
    ...
    
    parent.addToChildren(c1);
    
    session.save(parent);
    

    References

    • Hibernate Core Reference Guide
      • 1.2.6. Working bi-directional links
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a relation (one to many/ many to one) between parent and child
I have the following one-to-many relation between two objects. Parent --> IList<Child> Now, I
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have one to many relation between folders and docs tabels. I want to
I am learning django admin, i have to models with a one-to-many relation between
I want to enable something like a one-to-many relation between a text object and
I have the following Object PatientEligibilityObject It has One To Many relation with PatientsEligibilitiesDoctorsSpecialties
i have an entity Entity1 that have one to many relation with Entity2 as
I have a question about Criteria method, one-to-many relation to the database, 'one' is
I have a one-to-many relationship between two classes for this situation. I have a

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.