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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:50:05+00:00 2026-05-13T20:50:05+00:00

I’ve got the following situation: A User object has a Set of Permission objects

  • 0

I’ve got the following situation:

  • A User object has a Set of Permission objects (Set<Permission>)
  • Each User can have zero or more Permissions
  • A Permission object has three fields
  • The three fields of Permission make up the composite
    key for that permission.
  • As a consequence of this, we want exactly
    one instance in the DB of each Permission. Each user can potentially
    have the same Permission.
  • The User object therefore has a
    many-to-many relationship with Permission.

The question is: how, in this situation, do I make the Permission entity a composite key of itself? I’m particularly interested in doing this in the context of this many-to-many relationship.

Any ideas?

  • 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-13T20:50:05+00:00Added an answer on May 13, 2026 at 8:50 pm

    1º A Permission object has three fields

    2º The three fields of Permission up make up the composite key

    Both property and compound primary keys share the same columns

    So Your question looks like this one

    @Entity
    public class Permission {
    
        private PermissionId permissionId;
    
        private Integer field1;
        private Integer field2;
        private Integer field3;
    
        // required no-arg constructor   
        public Permission() {}
    
        public Permission(Integer field1, Integer field2, Integer field3) {
            this.field1 = field1;
            this.field2 = field2;
            this.field3 = field3;
    
            setPermissionId(new PermissonId(Integer field1, Integer field2, Integer field3));
        }
    
        @EmbeddedId
        public PermissionId getPermissionId() {
            return this.permissionId;
        }
    
        @Column(name="FIELD_1", insertable=false, updatable=false)
        public Integer getField1() {
            return this.field1;
        }
    
        @Column(name="FIELD_2", insertable=false, updatable=false)
        public Integer getField2() {
            return this.field2;
        }
    
        @Column(name="FIELD_3", insertable=false, updatable=false)
        public Integer getField3() {
            return this.field3;
        }
    
        @Embeddable
        public static class PermissionId implements Serializable {
    
            private Integer field1;
            private Integer field2;
            private Integer field3;
    
            // required no-arg constructor
            public PermissionId() {}
    
            public PermissionId(Integer field1, Integer field2, Integer field3) {
                this.field1 = field1;
                this.field2 = field2;
                this.field3 = field3;
            }
    
            @Column(name="FIELD_1", nullable=false)
            public Integer getField1() {
                return this.field1;
            }
    
            @Column(name="FIELD_2", nullable=false)
            public Integer getField2() {
                return this.field2;
            }
    
            @Column(name="FIELD_3", nullable=false)
            public Integer getField3() {
                return this.field3;
            }
    
            public boolean equals(Object o) {
                if(o == null) 
                    return false;
    
                if(!(o instanceof PermissionId))
                    return false;
    
                final PermissionId other = (PermissionId) o;
    
                if(!(getField1().equals(other.getField1()))) 
                    return false;
    
                if(!(getField2().equals(other.getField2()))) 
                    return false;
    
                if(!(getField3().equals(other.getField3()))) 
                    return false;
    
                return true;
            }
    
            // requered hashcode impl
            public int hashcode() {
                // code goes here     
            }
    
        }
    
    }
    

    But do not forget

    Because more than one property share the same column, you have to define one of them as insertable=false, updatable=false. Otherwise, Hibernate will complain some errors.

    And

    When you have a compound primary key, you have to set up its values. Hibernate does not support automatic generation of compound primary key.

    But if you do not like the approach as shown above, you can do the following one

    @Entity
    @IdClass(PermissionId.class)
    public class Permission {
    
        private Integer field1;
        private Integer field2;
        private Integer field3;
    
        // required no-arg constructor   
        public Permission() {}
    
        public Permission(Integer field1, Integer field2, Integer field3) {
            this.field1 = field1;
            this.field2 = field2;
            this.field3 = field3;
        }
    
        @Id
        @Column(name="FIELD_1", nullable=false)
        public Integer getField1() {
            return this.field1;
        }
    
        @Id
        @Column(name="FIELD_2", nullable=false)
        public Integer getField2() {
            return this.field2;
        }
    
        @Id
        @Column(name="FIELD_3", nullable=false)
        public Integer getField3() {
            return this.field3;
        }
    
        @Embeddable
        public static class PermissionId implements Serializable {
    
            private Integer field1;
            private Integer field2;
            private Integer field3;
    
            // required no-arg constructor
            public PermissionId() {}
    
            public PermissionId(Integer field1, Integer field2, Integer field3) {
                this.field1 = field1;
                this.field2 = field2;
                this.field3 = field3;
            }
    
            @Column(name="FIELD_1")
            public Integer getField1() {
                return this.field1;
            }
    
            @Column(name="FIELD_2")
            public Integer getField2() {
                return this.field2;
            }
    
            @Column(name="FIELD_3")
            public Integer getField3() {
                return this.field3;
            }
    
            public boolean equals(Object o) {
                if(o == null) 
                    return false;
    
                if(!(o instanceof PermissionId))
                    return false;
    
                final PermissionId other = (PermissionId) o;
    
                if(!(getField1().equals(other.getField1()))) 
                    return false;
    
                if(!(getField2().equals(other.getField2()))) 
                    return false;
    
                if(!(getField3().equals(other.getField3()))) 
                    return false;
    
                return true;
            }
    
            // requered hashcode impl
            public int hashcode() {
                // code goes here     
            }
    
        }
    
    }
    

    regards,

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

Sidebar

Related Questions

No related questions found

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.