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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:49:42+00:00 2026-05-16T22:49:42+00:00

I have a 1:n relation in the database and java objects that represent the

  • 0

I have a 1:n relation in the database and java objects that represent the relation with hibernate.
The primary key of the parent table is generated with a database sequence,
the child object uses the primary key of the parent object as foreign key.

I create a new parent object including a set of new child objects.
Then I try to persist all objects with one single

session.save(contract);

The storage of the parent object works without problems, but as soon as I add child objects I get an exception

… not-null property references a null or transient value …

because of the missing FK in the child object.

Is there a mapping which tells hibernate to use the newly generated PK of the parent object as FK in the child objects, or do I have to persist the child objects separately with the FK manually set?

mapping parent object:
…

@Id 
@Column(name="ID", unique=true, nullable=false, precision=22, scale=0)
@SequenceGenerator(name="sequence_seq", sequenceName="SEQ_CONTRACT")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_seq")
public long getId() {
    return this.id;
}

…

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="contract")
    public Set<ContractSearchtags> getContractSearchtagses() {
        return this.contractSearchtagses;
    }

…

mapping of child object

…

@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="CONTRACT_ID", nullable=false)
    public Contract getContract() {
        return this.contract;
    }

…

  • 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-16T22:49:43+00:00Added an answer on May 16, 2026 at 10:49 pm

    Is there a mapping which tells hibernate to use the newly generated PK of the parent object as FK in the child objects, or do I have to persist the child objects separately with the FK manually set?

    There is no particular mapping to use for this. However, I suspect that you’re not setting “both sides of the link” correctly when building your bidirectional association. You need to do something like this:

    Contract contract = new Contract();
    ...
    ContractSearchtags tag = new ContractSearchtags();
    ...
    contract.getContractSearchtagses().add(tag);
    tag.setContract(contract);
    
    session.save(contract);
    

    Developers typically use “link management methods” to correctly set both sides (in Contract):

    public void addToContractSearchtagses(ContractSearchtags tag) {
        contractSearchtagses.add(tag);
        tag.setContract(this);
    }
    

    And then the code becomes:

    Contract contract = new Contract();
    ...
    ContractSearchtags tag = new ContractSearchtags();
    ...
    contract.addToContractSearchtagses(tag);
    
    session.save(contract);
    

    Resources

    • 1.2.6. Working bi-directional links (this one is the more obvious)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables - child and parent with many-to-one relation. What
I have a relation mapping table like this: attributeid bigint productid bigint To clean
I understand that these methods are for pickling/unpickling and have no relation to the
i have in the database typical tables like user, usergroup, and they have relations.
I have two models: Person and Relation. The second one stores information about relations
when i have a many-to.many relation with nhibernate and let nhibernate generate my db
I have a pretty generic Article model, with m2m relation to Tag model. I
I have m2m field, lets say it have name 'relations', so i want to
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I
Have just started using Google Chrome , and noticed in parts of our site,

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.