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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:10:17+00:00 2026-06-18T11:10:17+00:00

I have a table which contains only 2 fields. The table has a composite

  • 0

I have a table which contains only 2 fields. The table has a composite PK formed by these two fields.

When using Netbeans for creating entity beans from database, the entity bean is not created automatically like other tables that have more than 2 fields.

So I guess I need to create the entity bean myself. What is the best practice for creating this entity bean? Does it have to contain COMPOSITE KEY object or not?

  • 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-18T11:10:18+00:00Added an answer on June 18, 2026 at 11:10 am

    I don’t use NetBeans so I can’t really say anything about its mapping tools.

    For mapping a composite key, there are a couple of options. You can

    • Define a separate @Embeddable object with the PK fields and use it as @EmbeddedId in your @Entity class

      @Embeddable
      public class MyCompositePK { 
          @Column
          private String fieldA;
          @Column
          private String fieldB;
      }
      @Entity 
      public class MyBean { 
          @EmbeddedId
          private MyCompositePK id;
          @Column
          private String fieldC;
      }
      
    • Define a non-mapped POJO with the PK fields and use it as @IdClass in the @Entity.

      @Entity
      @IdClass(value=ClassAB.ClassABId.class)
      public class ClassAB implements Serializable {
          private String idA;
          private String idB;
      
          @Id
          @Column(name="ID_A")
          public String getIdA(){ return idA; }
          public void setIdA(String idA){ this.idA = idA; }
      
          @Id
          @Column(name="ID_B")
          public String getIdB(){ return idB; }
          public void setIdB(String idB){ this.idB = idB; }
      
          static class ClassABId implements Serializable {
              private String idA;
              private String idB;
      
              public String getIdA(){ return idA; }
              public void setIdA(String idA){ this.idA = idA; }
      
              public String getIdB(){ return idB; }
              public void setIdB(String idB){ this.idB = idB; }
      
              // implement equals(), hashcode()
          }
      }
      

      In this example ClassABId is a static inner class just for convenience.

    These options are also explained in Pascal Thivent’s excellent answer to this question: How to map a composite key with Hibernate?.

    This related question discusses differences between these approaches: Which anotation should I use: @IdClass or @EmbeddedId. Notice the fields’ declaration gets duplicated with the @IdClass approach.

    Anyhow, I don’t think there’s an alternative to creating two classes. That’s why I asked this question : Mapping a class that consists only of a composite PK without @IdClass or @EmbeddedId. It seems there’s an hibernate-specific feature for this.

    As a side note, if you’ve got control over the DB structure, you might also consider avoiding composite keys. There are some reasons to do so.

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

Sidebar

Related Questions

I have a table which contains house details called property. I am creating a
I have a table which contains data about which node has been visited. It
I have table called FinalForgotten which only contains one field called aname. The field
I have two tables with hierarchyid fields, one of which is a staging table
I have a table which contains around 50,000 records of information which has been
I have a Statuses table which contains only an id and name field (Active,
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have a Table called Couple which has the following Fields CoupleId, HusbandId ,
I have table which contains double values stored in mysql database ...I need to
I have one table which contains events and dates, and another which contains the

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.