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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:22:07+00:00 2026-06-07T16:22:07+00:00

I have a classic one to many relationship and while saving it with Hibernate,

  • 0

I have a classic one to many relationship and while saving it with Hibernate, I am not able to pass parent’s PK column value to Child’s FK column.
Parent Class

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int holdingPK;

@OneToMany(mappedBy="holding",targetEntity=PolicyType.class,fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@XmlElement(name = "Policy")
private Set<PolicyType> policy;

Child Class

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int policyPK;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="HoldingFK",nullable = false)
private HoldingType holding;

Here HoldingPKis a auto generated sequence column which represents primary key. Value gets generated when I insert a Holding row. So I want to pass HoldingPK value to child’s HoldingFK column on the fly.
Test Code

HoldingType obj = new HoldingType();
obj.setCurrencyTypeCode("6");
obj.setHoldingKey("123");
Set<PolicyType> set = new TreeSet<PolicyType>();
PolicyType policy = new PolicyType();
policy.setJurisdiction("Haha");
set.add(policy);
obj.setPolicy(set);
session.save(obj);
transaction.commit();

So I am able to pass Child’s other values to Child Table column, just Parent PK is not reaching to Child’s FK column.

Here I am persisting XML document values to database. For this I am marshalling XML to Java Objects using JAXB then persisting objects using Hibernate. In this way I am reusing JAXB generated classes with Hibernate and these PK and FK elements do not exist on XML. These are specific to Database.

  • 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-07T16:22:09+00:00Added an answer on June 7, 2026 at 4:22 pm

    You simply forgot to initialise the owning side of the bidirectional association. You only initialized the inverse side (the one which has the mappedBy attribute). Hibernate only considers the owning side to know if an association exists or not (the side without the mappedBy attribute).

    Add this to your code (before the holding is saved):

    policy.setHolding(obj);
    

    Side note: your code would be much more readable if you named the policy field (and accessors) policies. There are many of them, so it should have a plural form.

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

Sidebar

Related Questions

I have a classic parent-collection of children relationship that is being used in a
I have a one to many relationship between EnquiryForm and UniversityFeeInstallment EnquiryForm has_many UniversityFeeInstallment.
In one of my projects, I have a very active classic ASP site with
I have a classic HTML select box: Show: <select name=show id=showThreads> <option value=all selected=selected>All</option>
I have a classic physics-thread vs. graphics-thread problem: Say I'm running one thread for
I have actually one classic Windows form and one button. I have this code
I have a bunch of old classic ASP pages, many of which show database
I have a DIV with many elements that are all clickable. Each one with
I have a classic sales database that contains millions of rows in certain tables.
I have a classic ASP application which inserts some values into a word file

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.