我有三个实体:
客户
它的复合PK为...... customer_id
和company_id
数据
ID:data_id
FK:area_id(来自下面的区域)
FK:customer_id(来自上方客户)
区域
ID:area_id
FK:company_id(来自上面的客户)
如何在JPA中创建@Join注释?我假设我必须使用@JoinTable,但我不知道该怎么做。
客户
@Entity
@Table(name="customer")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CustomerPK id;
//bi-directional many-to-one association to CustomColumnDataCustomer
@OneToMany(mappedBy="customer")
private List<CustomColumnDataCustomer> customColumnDataCustomers;
CustomerPK
@Embeddable
public class CustomerPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="customer_id")
private long customerId;
@Column(name="company_id")
private String companyId;
CustomColumnDataCustomer
@Entity
@Table(name="custom_column_data_customer")
@NamedQuery(name="CustomColumnDataCustomer.findAll", query="SELECT c FROM CustomColumnDataCustomer c")
public class CustomColumnDataCustomer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="custom_column_data_cust_uid")
private int customColumnDataCustUid;
//bi-directional many-to-one association to Customer
@ManyToOne
private Customer customer;
//bi-directional many-to-one association to AreaXCustomColumn
@ManyToOne
@JoinColumn(name="area_x_custom_column_uid")
private AreaXCustomColumn areaXCustomColumn;
AreaXCustomColumn
@Entity
@Table(name="area_x_custom_column")
@NamedQuery(name="AreaXCustomColumn.findAll", query="SELECT a FROM AreaXCustomColumn a")
public class AreaXCustomColumn implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="area_x_custom_column_uid")
private int areaXCustomColumnUid;
@Column(name="company_id")
private String companyId;
//bi-directional many-to-one association to CustomColumnDataCustomer
@OneToMany(mappedBy="areaXCustomColumn")
private List<CustomColumnDataCustomer> customColumnDataCustomers;
//bi-directional many-to-one association to CustomColumnDefinition
@ManyToOne
@JoinColumn(name="custom_column_definition_uid")
private CustomColumnDefinition customColumnDefinition;
答案 0 :(得分:0)
解决此问题的方法是使用注释@EmbeddedId
和@JoinColumn
。
我最近在一个项目上需要一个类似的解决方案。我认为如果我通过例子解释它会更容易:
我有三个对象,Platform
,EventMaster
和Membership
。
在这种情况下,Membership
是您的客户,它有两个PK,即平台ID和EventMaster ID(由@EmbeddedID
解决):
@EmbeddedId
private MembershipKey id;
MembershipKey
类只包含另一个类的PK:
@ManyToOne
@JoinColumn(name = "eventmaster_id")
private EventMaster eventMaster;
@ManyToOne
@JoinColumn(name = "mosplatform_id")
private MOSPlatform platform;
Platform
和EventMaster
类看起来都一样(这是Platform
类):
@OneToMany(mappedBy = "id.platform")
private List<Membership> memberships;
我认为这可以帮助您制定解决方案。
编辑:问题中的代码已编辑。