简体   繁体   中英

Jpa mapping of composite key

Hi Below is my tables and its desc.

Employee:
emp_Id primary_key
emp_Name 
emp_Address

Address:
emp_id -- foreign key of employee table
addres_type
type_id

AddressType:
Type_id  -- foreign key of Address table
Type
Desc

How can i create JPA entities with this relationship.

Thanks in advance

you'd better use AbstractPersistable to generate the id :

so Address and AddressType will have a primary_key also

1-Employee

 @Entity
    @Table(name = "Employee ")
    public class Employee extends AbstractPersistable<Long>{

// Employee here have a generated id from AbstractPersistable

        private static final long serialVersionUID = 1L;

        @Column(name = "emp_Name ")
        private String emp_Name ;


        @ManyToOne(optional = true)
        @JoinColumn(name = "emp_Address")
        private Address emp_Address;
    }

2-Address

 @Entity
    @Table(name = "Address")
    public class Address extends AbstractPersistable<Long>{

    //Address here have a generated id from AbstractPersistable

        private static final long serialVersionUID = 1L;

//you can delete this column because you have already the ManyToOne in Employee 
        @ManyToOne(optional = true)
        @JoinColumn(name = "emp_id")
        private Employee emp_id;
//*****************

        @Column(name = "addres_type")
        private String addres_type;


        @ManyToOne(optional = true)
        @JoinColumn(name = "Type_id")
        private AddressType Type_id  ;
    }

3-AddressType

  @Entity
        @Table(name = "AddressType")
        public class AddressType extends AbstractPersistable<Long>{

       //AddressType here have a generated id from AbstractPersistable

            private static final long serialVersionUID = 1L;



            @Column(name = "Type")
            private String Type;

            @Column(name = "Desc")
            private String Desc;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM