gpt4 book ai didi

java - 组合键的 Jpa 映射

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:23 24 4
gpt4 key购买 nike

您好,下面是我的表格及其说明。

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

关于java - 组合键的 Jpa 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43652726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com