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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:58:57+00:00 2026-05-16T09:58:57+00:00

I have two table Part and SubPart. Part table has general fields like id,

  • 0

I have two table Part and SubPart. Part table has general fields like id, name, desc etc. The SubPart table has part_id, sub_part_id as composite key. Both of these columns are referring to Part table and has a one to many mapping for each of them, like for each part_id in the Part table there can be multiple entries in SubPart table for both the columns. I’m having problem defining the composite key for the SubPart table. I tried the Embedded tag but its not working. How can I address this problem. Thanks a lot.

Part table like this.

@Entity
@Table(name="Part")
public class Part {

    @Id
    @GeneratedValue
    @Column(name="Part_Id")
    private int id;
    @Column(name="Part_Number")
    private String partNumber;
    @Column(name="Part_Name")
    private String partName;
}

Sub Part Table

@Entity
@Table(name="SubPart")
public class SubPart {
    // part and subPart combination is the compound key here.
    @ManyToOne
    @JoinColumn(name="Part_Id")
    private Part part;

    @ManyToOne
    @JoinColumn(name="Sub_Part_Id")
    private Part subPart;

    @Column(name="Quantity")
    private Integer quantity;
}
  • 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-16T09:58:57+00:00Added an answer on May 16, 2026 at 9:58 am

    You said

    I’m having problem defining the composite key for the SubPart table

    When you have a compound primary key, you must define a class (Usually a static inner class) which defines your compound primery key (Just an advice: because Hibernate makes use of proxies, prefer to put your annotated mapping on the getter’s instead of field members)

    /**
      * When both entity class and target table SHARE the same name
      * You do not need @Table annotation
      */
    @Entity
    public class SubPart implements Serializable {
    
        @EmbeddedId
        private SubPartId subPartId;
    
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="PART_ID", insertable=false, updateable=false)
        private Part part;
    
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="SUP_PART_ID", insertable=false, updateable=false)
        private SubPart subPart;
    
        /**
          * required no-arg constructor
          */
        public SubPart() {}
        public SubPart(SubPartId subPartId) {
            this.subPartId = subPartId;
        }
    
        // getter's and setter's
    
        /**
          * It MUST implements Serializable
          * It MUST overrides equals and hashCode method
          * It MUST has a no-arg constructor
          *
          * Hibernate/JPA 1.0 does not support automatic generation of compound primary key
          * You SHOULD set up manually
          */
        @Embeddable
        public static class SubPartId implements Serializable {
    
            @Column(name="PART_ID", updateable=false, nullable=false)
            private Integer partId;
            @Column(name="SUB_PART_ID", updateable=false, nullable=false)
            private Integer subPartId;
    
            /**
              * required no-arg constructor
              */
            public SubPartId() {}
            public SubPartId(Integer partId, Integer subPartId) {
                this.partId = partId;
                this.subPartId = subPartId;
            }
    
            // getter's and setter's
    
            @Override
            public boolean equals(Object o) {
                if(!(o instanceof SubPartId))
                    return null;
    
                final SubPartId other = (SubPartId) o;
                return new EqualsBuilder().append(getPartId(), other.getPartId())
                                          .append(getSubPartId(), other.getSubPartId())
                                          .isEquals();
            }
    
            @Override
            public int hashCode() {
                return new HashCodeBuilder().append(getPartId())
                                            .append(getSubPartId())
                                            .toHashCode();  
            }
    
        }
    
    }
    

    Notice Part and SubPart mapping has been marked as insertable=false, updateable=false Because the mapping has been defined in the compound primary key. Hibernate does not allow you mapping two properties WITH THE SAME COLUMN unless you mark insertable=false, updateable=false. Otherwise you will see this nice exception

    Should be marked with insertable=false, updateable=false

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

Sidebar

Ask A Question

Stats

  • Questions 518k
  • Answers 518k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not an exact duplicate, hence not flagged as such, but… May 16, 2026 at 8:19 pm
  • Editorial Team
    Editorial Team added an answer If you are running a thread calling finish() to your… May 16, 2026 at 8:19 pm
  • Editorial Team
    Editorial Team added an answer I'm not going to guess (as per @Tomasz :), but… May 16, 2026 at 8:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have two table fields in a MySQL table. One is VARCHAR and is
Let's say we have two tables: 'Car' and 'Part', with a joining table in
I have two tables like this Table Product ProductId - int <PK> ProductExpiry -
I have got two table create table t1(cid int, isnews int) create table t2(nid
I have two tables, I want to search TermID in Table-A through TermID in
I am using EF (Framework 3.5 SP1) and have a simple two table demo
I have a two part problem that needs fixing. I'll try my best to
Urgh...my LINQ has suddenly taken a turn for the worse!! I have two tables
If I define a MySQL index over two fields, how do I find out,

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.