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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:22:02+00:00 2026-06-13T01:22:02+00:00

OK, I’ve tried everything, just can’t make this to work: I have the following

  • 0

OK, I’ve tried everything, just can’t make this to work:

I have the following table:

  • user_id int , references users.id
  • platform_id int , references platforms.id
  • field1
  • field2
  • …

My primary key (in th DB and logically) is the combination of user_id and platform_id.

I want to create a composite key that will have the User and Platform classes as members (like I would do with a regular OneToOne or OneToMany relations).

I want the composite key to have the actual classes references and not only the ids (much easier for me later when I need the actual reference for some calculations).

Furthermore, I need the User class to have a OneToMany relation with this UserPlatformData class.

I was able to create an Embeddable class with User and Platform but JPA always does insert and never updates the existing records even when the pk is the same (that leads to duplicate entry for key primary...).

How do I create a class with a primary key that consists of two other references (not primitives)?

  • 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-13T01:22:03+00:00Added an answer on June 13, 2026 at 1:22 am
    1. Declare your entity, using the @IdClass annotation and two @Id annotations. There are two options for the id field types:
      (i) basic types: matching the DB columns OR
      (ii) non-basic types: entity objects referenced as @ManyToOne via a FK, allowing FK relationships to be part of the PK.
      You have said you want this second case:

        @Entity   
        @IdClass(UserPlatformId.class)       
        public class UserPlatform {  
      
             @Id 
             @ManyToOne
             User user;
             @Id
             @ManyToOne
             Platform platform;
      
          // ...
      
       }
      
    2. Declare a javabean id class to describe the composite PK fields.

      • The field types must be basic types here, matching the underlying DB columns, which will be the same basic types as the @Id field of User class and the @Id field of Platform class. Here basic types are always used, even if you used the non-basic types for entity @Id fields (ii above).
      • The field names in the Id Class must match the field names in the entity.
        The @IdClass thus acts as a form of mapping from non-basic types in the entity to basic types in the DB:
    public class UserPlatformId {
    
          int user;   
          int platform;
    
          public UserPlatformId();
          public UserPlatformId(int userId, int platformId) { 
                this.user=userId; this.platform=platformId; }
    
          public int getUser() { // ...};
          public int getPlatform() { // ...};
    }
    

    You can leave out setter methods, and instead have an extra constructor to set fields up front. Once created, the Id should be immutable.

    Good to go =:-)

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.