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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:02:14+00:00 2026-06-05T16:02:14+00:00

I have two classes Foo and Bar . The tables in the database look

  • 0

I have two classes Foo and Bar. The tables in the database look like this:

|Foo|
|id : INT (PK) | bar_id : INT (PK, FK) |

|Bar|
|id : INT (PK) |

Normally I would map it like this:

@Entity
public class Bar
{
    @Id
    @Column(name = "id")
    private int id;

    @OneToMany
    private Set<Foo> foo;
}

@Entity
public class Foo
{
    @EmbeddedId
    private FooPK key;

    @MapsId("barId")
    @ManyToOne
    @JoinColumn(name = "bar_id", referencedColumnName = "id")
    private Bar bar;
}

@Embeddable
public class FooPK
{
    @Column(name = "id")
    private int id;
    @Column(name = "bar_id")
    private int barId;
}

However the id’s in FooPK are loosely mapped and need to be connected manually. I would prefer a solution that maps using Objects in stead of loose ids.
I tried the following but (of course) it didn’t work, but I think it gives an idea of what I would like to achieve:

@Entity
public class Bar
{
    @Id
    @Column(name = "id")
    private int id;

    @OneToMany
    private Set<Foo> foo;
}

@Entity
public class Foo
{
    @EmbeddedId
    private FooPK key;

    @MapsId("barId")
    @ManyToOne
    @JoinColumn(name = "bar_id", referencedColumnName = "id")
    @Access(AccessType.FIELD)
    private Bar getBar()
    {
        return key.getBar();
    }
}

@Embeddable
public class FooPK
{
    @Column(name = "id")
    private int id;

    @Transient
    private Bar bar;

    //....

    @Column(name = "bar_id")
    @Access(AccessType.PROPERTY)
    private int getBarId
    {
        return bar.getId();
    }
}

Another problem with the latter solution is that the getBarId() method in FooPK needs to have a setBarId(Int) method. Setting the Object using the ID can be done by accessing the data access layer, however this (in my opinion) violates the separation of business/domain/data layers.

So what to do? Go with the first solution and keep the ids in sync manually or is there another (best) practice?

  • 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-05T16:02:15+00:00Added an answer on June 5, 2026 at 4:02 pm

    Referring to the JPA 2: composite primary key classes discussion, make the following changes to the Foo and FooPK classes:

    @Entity
    public class Foo {
    
        @EmbeddedId
        private FooPK key;
    
        @MapsId("barId") //references EmbeddedId's property
        @JoinColumn(name = "bar_id", referencedColumnName = "id")
        @ManyToOne
        private Bar bar;
    }
    
    @Embeddable
    public class FooPK {
    
        @Column(name = "id")
        private int id;
        @Column(name = "bar_id")
        private int barId;
    }
    

    I suggest first making it work with FIELD access and then apply PROPERTY access.

    So what to do? Go with the first solution and keep the ids in sync
    manually or is there another (best) practice?

    Save yourself from pain – generate ids automatically.

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

Sidebar

Related Questions

I have two classes, Foo and Bar, that have constructors like this: class Foo
So I have two classes like this: class foo { /* code here */
I have two classes Foo and Bar mapped to two different tables, and I'd
Say I have two independent classes: class Foo { int bar; } class Baz
Given two model classes, Foo and Bar , I want Foo to have 3
I have two classes that I would like to merge into a composite. These
I have two classes, Foo<T> and Bar<T> , derived from Base . Each overrides
I have two classes: public class Foo { public int FooId {get;set;} public virtual
I have the following two classes. public class Foo { public Bar Args {
I have two wars, foo.war and bar.war. foo uses classes from bar. I'm trying

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.