嗨,下面是我的桌子和它的描述。

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


如何创建具有这种关系的JPA实体。

提前致谢

最佳答案

您最好使用AbstractPersistable生成id:

因此Address和AddressType也将具有primary_key

1名员工

 @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地址

 @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地址类型

  @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;
        }

10-08 16:50