How to map a composite key with JPA and Hibernate?

前端 未结 8 1141
旧时难觅i
旧时难觅i 2020-11-22 10:37

In this code, how to generate a Java class for the composite key (how to composite key in hibernate):

create table Time (
     levelStation int(15) not null,         


        
8条回答
  •  失恋的感觉
    2020-11-22 11:01

    Using hbm.xml

        
    
            
            
            
          
    

    Using Annotation

    Composite Key Class

    public  class PK implements Serializable{
        private int PRODUCT_Product_ID ;    
        private int categories_id ;
    
        public PK(int productId, int categoryId) {
            this.PRODUCT_Product_ID = productId;
            this.categories_id = categoryId;
        }
    
        public int getPRODUCT_Product_ID() {
            return PRODUCT_Product_ID;
        }
    
        public void setPRODUCT_Product_ID(int PRODUCT_Product_ID) {
            this.PRODUCT_Product_ID = PRODUCT_Product_ID;
        }
    
        public int getCategories_id() {
            return categories_id;
        }
    
        public void setCategories_id(int categories_id) {
            this.categories_id = categories_id;
        }
    
        private PK() { }
    
        @Override
        public boolean equals(Object o) {
            if ( this == o ) {
                return true;
            }
    
            if ( o == null || getClass() != o.getClass() ) {
                return false;
            }
    
            PK pk = (PK) o;
            return Objects.equals(PRODUCT_Product_ID, pk.PRODUCT_Product_ID ) &&
                    Objects.equals(categories_id, pk.categories_id );
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(PRODUCT_Product_ID, categories_id );
        }
    }
    

    Entity Class

    @Entity(name = "product_category")
    @IdClass( PK.class )
    public  class ProductCategory implements Serializable {
        @Id    
        private int PRODUCT_Product_ID ;   
    
        @Id 
        private int categories_id ;
    
        public ProductCategory(int productId, int categoryId) {
            this.PRODUCT_Product_ID = productId ;
            this.categories_id = categoryId;
        }
    
        public ProductCategory() { }
    
        public int getPRODUCT_Product_ID() {
            return PRODUCT_Product_ID;
        }
    
        public void setPRODUCT_Product_ID(int PRODUCT_Product_ID) {
            this.PRODUCT_Product_ID = PRODUCT_Product_ID;
        }
    
        public int getCategories_id() {
            return categories_id;
        }
    
        public void setCategories_id(int categories_id) {
            this.categories_id = categories_id;
        }
    
        public void setId(PK id) {
            this.PRODUCT_Product_ID = id.getPRODUCT_Product_ID();
            this.categories_id = id.getCategories_id();
        }
    
        public PK getId() {
            return new PK(
                PRODUCT_Product_ID,
                categories_id
            );
        }    
    }
    

提交回复
热议问题