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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:11:56+00:00 2026-06-06T04:11:56+00:00

How do I implement the following relationship using JPA? table person ( id int,

  • 0

How do I implement the following relationship using JPA?

table person (
  id int,
  name text
)

table person_home (
    person_id int,
    home_id int,
    type char(1)       -- 'p' = primary, 's' = secondary
)

table home (
    id int,
    address text
)

A person can have many homes, and a home can have many persons living in it (i.e. ManyToMany relationship).
Furthermore, a home can be a primary residence for one person, but a secondary residence for another person at the same time.

I’m not sure how to model this relationship, even though the database schema is clear.

I have thought of splitting mapping table person_home into person_primary_home and person_secondary_home, however I would prefer to retain the schema if possible.

  • 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-06T04:11:58+00:00Added an answer on June 6, 2026 at 4:11 am

    This question is pretty much asked and answered here:
    How to create a composite primary key which contains a @ManyToOne attribute as an @EmbeddedId in JPA?

    You need four classes:

    1. Person.java
    2. Home.java
    3. PersonHome.java
    4. PersonHomePk.java

    The Person.java and Home.java files you create with one to many relations to the PersonHome.java. They’ll have @Id fields to identify the primary keys. Each will have a @OneToMany relation defined with at least a mappedBy attribute mapping to their respective fields in the PersonHome entity. i.e. in the Person.java you could have something like

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "Person")
    private Collection<PersonHome> personHome;
    

    The PersonHome.java will have an @EmbeddedId field to identify the PersonHomePk instance declaration which is its primary key (that is, instead of an @Id column you will have an @EmbeddedId annotating a declaration of a class representing the primary key of the join table PersonHome). Any other fields are declared as normal columns. The PersonHome.java will also declare two ManyToOne relations one each to person and home. These will use @JoinColumn annotation (make sure they have the attributes insertable=false and updatable=false). The datatypes will be the Person and Home classes. i.e.

    @EmbeddedId
    protected PersonHomePk personHomePk;
    @Column (name = "type")
    private String type; 
    @JoinColumn(name = "person_id", referencedColumnName = "person_id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Person person;
    

    You’ll need the same for the “Home” declaration too.

    Why are you using only a char for “type”. I’d recommend a varchar so people who maintain the thing once you’re gone will understand the code and database better when you aren’t around. ‘detached’ is easier to understand the ‘d’.

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

Sidebar

Related Questions

i have the following entity relationship: SideA: @Entity @Table(name = SideA) public class SideA
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint
I tried to implement the following in less: nav > ul > li:first-child using:
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
I'm tring to implement LINQ to Entity using Include extension. I have the following
we need to implement the following query in such a way that we should
How can I implement the following query with Query Builder? SELECT * FROM t
I would like to implement the following behaviour: I have a special media player,
I would like to implement the following simple pattern in an android app: Activity
Can someone please tell me how I can implement the following line of pseudo-code.

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.