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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:39:41+00:00 2026-06-09T07:39:41+00:00

I am persisting a parent-child object using Hibernate. Here Parent comes with an id

  • 0

I am persisting a parent-child object using Hibernate. Here Parent comes with an id which is a primary key from the sender system and always be unique.
For every new incoming object with parent id doesn’t exist then parent object will be inserted in Parent Table with my application database specific primary key ParentPK and child rows will be inserted with corresponding ParentFK .
If parent id already exist in my application database then I have to only update the Parent Table. But How I am supposed to insert ParentFK for the child rows if ParentPK already exists ?
Table Structure:

CREATE TABLE Parent(
    ParentPK bigint NOT NULL,
    TypeCode int NULL,
    Id bigint NULL,
    FullName varchar(50) NULL
}

CREATE TABLE Child(
    ChildPK bigint NOT NULL,
    Code int NULL,
    Designation int NULL,
    ParentFK bigint NULL
}

ALTER TABLE Child ADD
  CONSTRAINT FK_Child_Parent FOREIGN KEY(ParentFK)
    REFERENCES Parent (ParentPK)

Entity Classes:

@Entity
@Table(name="Parent")
public class ParentType extends HibernateEntity{


    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="KeyGenerator")
    @GenericGenerator(name = "KeyGenerator",
        strategy = "services.db.hibernate.KeyGenerator")
    protected Long parentPK;

    protected String id;
    protected int typeCode;
    protected String fullName;

    @OneToMany(mappedBy="parent",targetEntity=ChildType.class,fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    protected List<ChildType> child;
}

@Entity
@Table(name="Child")
public class ChildType extends HibernateEntity{

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="KeyGenerator")
    @GenericGenerator(name = "KeyGenerator",
        strategy = "services.db.hibernate.KeyGenerator")
    protected Long childPK;
    protected int code;
    protected int designation;

    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="ParentFK")
    protected ParentType parent;
}
  • 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-09T07:39:43+00:00Added an answer on June 9, 2026 at 7:39 am

    In Hibernate (and JPA) you don’t work primarily with IDs but with instances of objects. So instead of setting the ID of the parent, you need to load an instance of ParentType and then set it into an instance of ChildType.

    In JPA (I’m more used to JPA) it would be something like:

    long parentId = ...; // you get this from the sender system
    ParentType parent =
        entityManager.find(ParentType.class, parentId);
    // Now you can set the parent to instances of ChildType,
    // and Hibernate will store the correct ID into the database.
    

    In Hibernate it would be something like

    ...
    ParentType parent =
        (ParentType)session.get(ParentType.class, parentId);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to import data from a database that uses primary key /
Taken from here: http://docs.jboss.org/hibernate/stable/core/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode I tend to use List since Criteria returns List ,
If i create a new Parent object and then add new child object to
Using the peristance manager, how can I retrieve a child object knowing a child
I have a parent-child relationship, such as Order to Order Item, which I have
I'm using the following snippet: $('input[value=NEW]:hidden').attr('value', '').parent().parent().animate({ backgroundColor: #FF9933, duration: 800 }).delay(500).animate({ duration: 800,
I would like to use hibernate for persisting objects through web services and am
I have a class called Parent , and I'm storing these object's in the
My model is a simple parent/child representation looking like this public class Parent {
We are using Spring/hibernate with OnetoMany/ManytoOne relationships. We are using HQL save methods to

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.