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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:27:54+00:00 2026-05-24T00:27:54+00:00

I’m working with an existing schema that I’d rather not change. The schema has

  • 0

I’m working with an existing schema that I’d rather not change. The schema has a one-to-one relationship between tables Person and VitalStats, where Person has a primary key and VitalStats uses the same field as both its primary key and its foreign key to Person, meaning its value is the value of the corresponding PK of Person.

These records are created by external processes, and my JPA code never needs to update VitalStats.
For my object model I’d like my Person class to contain a VitalStats member, BUT:

When I try

@Entity
public class Person{
    private long id;
    @Id
    public long getId(){ return id; }

    private VitalStats vs;
    @OneToOne(mappedBy = “person”)
    public VitalStats getVs() { return vs; }
}

@Entity
    public class VitalStats{
     private Person person;
    @OneToOne
    public Person getPerson() { return person; }
}

I have the problem that VitalStats lacks an @Id, which doesn’t work for an @Entity.\

If I try

@Id @OneToOne
public Person getPerson() { return person; }

that solves the @Id problem but requires that Person be Serializable. We’ll get back to that.

I could make VitalStats @Embeddable and connect it to Person via an @ElementCollection, but then it would have to be accessed as a collection, even though I know that there’s only one element. Doable, but both a little bit annoying and a little bit confusing.

So what’s preventing me from just saying that Person implements Serializable? Nothing, really, except that I like everything in my code to be there for a reason, and I can’t see any logic to this, which makes my code less readable.

In the meantime I just replaced the Person field in VitalStats with a long personId and made that VitalStats’s @Id, so now the @OneToOne works.

All of these solutions to what seems (to me) like a straightforward issue are a bit clunky, so I’m wondering whether I’m missing anything, or whether someone can at least explain to me why Person has to be Serializable.

TIA

  • 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-24T00:27:55+00:00Added an answer on May 24, 2026 at 12:27 am

    To map one-to-one association using shared primary keys use @PrimaryKeyJoinColumn and @MapsId annotation.

    Relevant sections of the Hibernate Reference Documentation:

    PrimaryKeyJoinColumn

    The PrimaryKeyJoinColumn annotation does say that the primary key of
    the entity is used as the foreign key value to the associated entity.

    MapsId

    The MapsId annotation ask Hibernate to copy the identifier from
    another associated entity. In the Hibernate jargon, it is known as a
    foreign generator but the JPA mapping reads better and is encouraged

    Person.java

    @Entity
    public class Person {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "person_id")
        private Long id;
    
        @OneToOne(cascade = CascadeType.ALL)
        @PrimaryKeyJoinColumn
        private VitalStats vitalStats;       
    }
    

    VitalStats.java

    @Entity
    public class VitalStats 
    {
        @Id @Column(name="vitalstats_id") Long id;
    
        @MapsId 
        @OneToOne(mappedBy = "vitalStats")
        @JoinColumn(name = "vitalstats_id")   //same name as id @Column
        private Person person;
    
        private String stats;
    }
    

    Person Database Table

    CREATE TABLE  person (
      person_id   bigint(20) NOT NULL auto_increment,
      name        varchar(255) default NULL,
      PRIMARY KEY  (`person_id`)
    ) 
    

    VitalStats Database Table

    CREATE TABLE  vitalstats 
    (
      vitalstats_id  bigint(20) NOT NULL,
      stats          varchar(255) default NULL,
      PRIMARY KEY  (`vitalstats_id`)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an existing ASP.NET MVC app that started small and has
I am working on some schema changes to an existing database. I backed up
I'm working with an existing XML document which has a structure (in part) like
I'm working on some existing c++ code that appears to be written poorly, and
I have been working with an existing website out company has running until I
I'm working on an upgrade for an existing database that was designed without any
I'm working with an existing database schema, and trying to setup two Doctrine models
I use MongoDB in one of my Java projects. After a DB schema change,
I am working on a legacy code base with an existing DB schema. The
i'm working with an existing client-side legacy database that we're converting to MySQL for

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.