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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:25:27+00:00 2026-05-13T05:25:27+00:00

I want a unidirectional one-to-one relationship between objects of 3 java classes: Person to

  • 0

I want a unidirectional one-to-one relationship between objects of 3 java classes: Person to Heart, and Person to Liver. I want the objects to share the same PK i.e. every person has a corresponding heart and liver, where person.person_id = heart.heart_id = liver.liver_id. I do not want to merge the 3 tables into 1 because each has loads of fields. Here’s my code (mostly based on the accepted answer for this question):

@Entity
public class Person {
   public long personId;
   private String name;
   public Heart heart;
   public Liver liver;
   // other fields

   @Id
   @GeneratedValue
   public long getPersonId() {return personId;}

   @OneToOne(cascade = CascadeType.ALL)
   @PrimaryKeyJoinColumn
   public Heart getHeart() {return heart;}

   @OneToOne(cascade = CascadeType.ALL)
   @PrimaryKeyJoinColumn
   public Liver getLiver() {return liver;}

   // other getters and setters and constructors
}


@Entity
public class Heart {
   private long heartId;
   private int bpm;
   private Person person;
   // other fields

   @Id
   @GenericGenerator(
      name = "generator",
      strategy = "foreign",
      parameters = @Parameter(name = "property", value = "person")
   )
   @GeneratedValue(generator = "generator")
   public long getHeartId() {return heardId;}

   @OneToOne(mappedBy="heart")
   @PrimaryKeyJoinColumn
   public Person getPerson() {return person;}

   // other getters and setters and constructors
}


@Entity
public class Liver {
   private long liverId;
   private boolean healthy;
   private Person person;
   // other fields

   // the rest uses the same hibernate annotation as Heart
}

I setup the session and do the following:

Person jack = new Person();
jack.setName("jack");
Heart heart = new Heart();
heart.setBpm(80);
Liver liver = new Liver();
liver.setHealthy(true);

Then if I link up the person object with it’s organs, and save it, I get an error (NOTE: I got the same behaviour when I just used 2 classes e.g. Person and Heart):

jack.setHeart(heart);
jack.setLiver(liver);
session.save(jack);

org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: person

However it works if I set the relationship both ways:

jack.setHeart(heart);
heart.setPerson(jack);
jack.setLiver(liver);
liver.setPerson(jack);
session.save(jack);

But surely this should not be necessary for unidirectional relationships?
Cheers

ps. Oddly enough, I notice it works (saves both objects to the DB) when I just use 2 classes e.g. Person and Heart, and I just set the link the other way:

heart.setPerson(jack);
session.save(heart);

I have no idea why this works (it seems logical to me that Person is the parent object, as it auto-generates it’s own PK, and the others use that; so that’s all you should have to setup), but anyway I cannot figure out how to apply this working method to my 3-class 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-13T05:25:27+00:00Added an answer on May 13, 2026 at 5:25 am

    I hate to tell you this, but you have a bi-directional relationship there. The Person has a reference to the Heart and Liver and each of those have a reference back to the Person. The annotations that you have set up on the Id properties of your Heart and Liver are specifically saying that they get the value of their Id property by delegating to their Person property. In the examples that you’ve shown that don’t work, you haven’t set the Person property on those guys yet and so, they obviously cannot obtain their Id value.

    You can either set this relationship up as a true unidirectional OneToOne, which is documented in the Hibernate annotations documentation:

    @Entity
    public class Body {
        @Id
        public Long getId() { return id; }
    
        @OneToOne(cascade = CascadeType.ALL)
        @PrimaryKeyJoinColumn
        public Heart getHeart() {
            return heart;
        }
        ...
    }
    
    
    @Entity
    public class Heart {
        @Id
        public Long getId() { ...}
    }
    

    or you can change our entity objects slightly to streamline hooking up both sides of the relationship such as:

    @Entity
    public class Person {
       public long personId;
       private String name;
       public Heart heart;
       public Liver liver;
       // other fields
    
       @Id
       @GeneratedValue
       public long getPersonId() {return personId;}
    
       @OneToOne(cascade = CascadeType.ALL)
       @PrimaryKeyJoinColumn
       public Heart getHeart() {return heart;}
    
       public void setHeart(Heart heart){
          this.heart = heart;
          this.heart.setPerson(this);
       }
    
       @OneToOne(cascade = CascadeType.ALL)
       @PrimaryKeyJoinColumn
       public Liver getLiver() {return liver;}
    
       public void setLiver(Liver liver){
          this.liver = liver;
          this.liver.setPerson(this);
       }
       // other getters and setters and constructors
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.