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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:14:35+00:00 2026-05-15T22:14:35+00:00

I am having trouble setting up jpa mappings for some entities. I have a

  • 0

I am having trouble setting up jpa mappings for some entities. I have a parent entity defined like the following.

@Entity
@Table(name="EIF_INSTANCE_HDR")
public class InstanceEntity implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="eif_inst_gen")
    @SequenceGenerator(name="eif_inst_gen",sequenceName="EIF_INSTANCE_SEQ")
    @Column(name = "EAIH_ID")
    private Long eaihid;

    @Column(name = "EAD_ID")
    private Long eadid;

    @OneToMany(targetEntity=InstanceNotifyEntity.class, mappedBy="instance",fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    private List<InstanceNotifyEntity> userDetails = new ArrayList<InstanceNotifyEntity>();

}

I then have a child entity w/ a composite key, and a foreign key to the primary key of this table as follows:

@Entity
@Table(name="EIF_INST_NOTIFIED")
public class InstanceNotifyEntity implements Serializable{

    private static final long serialVersionUID = 1L;
    
    @Id
    @ManyToOne
    @JoinColumn(name="EAIH_ID", referencedColumnName="EAIH_ID")
    private InstanceEntity instance;
    
    @Id
    @Column(name="USER_ID")
    private Long userId;

    @Column(name="COMMENT_TXT")
    private String commentText;

}

I know the child entity is incorrect, but I am unsure how to set this up to have a composite PK. I know I need to setup a PK class, but I am not sure how to do that when one field is a foreign key to the parent class. And once that is setup how would the parent reference the child entity?

Any help is appreciated.

  • 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-15T22:14:35+00:00Added an answer on May 15, 2026 at 10:14 pm

    This is governed by JPA 2 spec section 2.4.1, “Primary Keys Corresponding to Derived Identities”. The section contains two examples directly applicable to your problem.

    As described in the spec, there are two ways to represent the child entity’s key in this case:

    • @IdClass
    • @EmbeddedId

    Here’s a rough sketch of the EmbeddedId way. I chose EmbeddedId arbitrarily, but the choice between IdClass and EmbeddedId is significant. You might choose differently.

    // Child entity's composite primary key
    @Embeddable
    public class InstanceNotifyEntityId implements Serializable {
        Long eaihId;
        Long userId;
    }
    
    // Child entity
    @Entity
    @Table(name="EIF_INST_NOTIFIED")
    public class InstanceNotifyEntity implements Serializable {
        @AttributeOverrides({
          @AttributeOverride(name="userId", column = @Column(name="USER_ID"))
          @AttributeOverride(name="eaihId", column = @Column(name="EAIH_ID"))
        })
        @EmbeddedId
        InstanceNotifyEntityId id;
    
        @MapsId("eaihId")
        @ManyToOne
        InstanceEntity instance;
    
        // ...
     }
    

    The parent entity needs one change: the userDetails attribute mappedBy should be “id.eaihId”. I think that’s it, but I haven’t used entities exactly like this before. Might have missed something… please post if you see errors.

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

Sidebar

Related Questions

I am having trouble setting the type of a String, it goes like public
I'm having trouble setting up ruby on rails on my machine. I've been following
Im having trouble setting up a development environment. I need some tutorial on how
I'm having trouble setting up Teamcity and Git. I have a git repo checked
I'm having trouble setting 2 positions on a scroll function using offset. I have
I am having trouble setting this up for multiple divs. I have it here
I am having trouble setting up a Fluent NHibernate HasMany collection. I have set
I am having trouble setting up landscape mode in my application. I have a
I'm having trouble setting up a cron job. I have a cron job created
I have been having trouble setting a box shadow on the left side of

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.