复合键的Jpa映射

时间:2017-04-27 08:37:02

标签: java database jpa entity-relationship openjpa

嗨下面是我的桌子和它的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

如何使用此关系创建JPA实体。

提前致谢

1 个答案:

答案 0 :(得分:0)

您最好使用 AbstractPersistable 来生成ID:

所以Address和AddressType也有一个primary_key

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

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