模型 java class 作为 JPA 中的连接结果
Model java class as joined result in JPA
考虑以下数据。这是一个虚构的在线商店系统。 Surcharge
根据不同的商品类别(食物或衣服)和支付方式(现金或信用卡)存储附加费配置。 Packaging
根据商品类别和付款方式存储可用的包装选项。如果您用现金支付食物,抱歉没有优质包装给您。
- 下划线表示主键。
Surcharge
和Packaging
是一对多的关系。我的情况是,我不想拥有 4 个 Surcharge
对象实例,每个实例都有一个 List<Packaging>
。我想要 7 个对象,每个对象都有 category
、paymentMeans
、surcharge
、option
的副本。就像他们加入时一样,结果将是 7 行。如何为我的 java class 建模以实现此目的?谢谢。
@Entity
@Table(name = "SURCHARGE")
public class Surcharge implements Serializable {
@EmbeddedId
private SurchargeId surchargeId;
@Column(name = "SURCHARGE")
private int surcharge;
// how to map this attribute with packaging table as a single instance?
private Packaging packaging;
}
@Embeddable
public class SurchargeId implements Serializable {
@Column(name = "CATEGORY")
private String category;
@Column(name = "PAYMENT_MEANS")
private String paymentMeans;
}
@Entity
@Table(name = "PACKAGING")
public class Packaging implements Serializable {
@EmbeddedId
private PackagingId packagingId;
}
@Embeddable
public class PackagingId implements Serializable {
@Column(name = "CATEGORY")
private String category;
@Column(name = "PAYMENT_MEANS")
private String paymentMeans;
@Column(name = "OPTION")
private String option;
}
在Surcharge
中,总会有Packaging
的列表。你可以做的是在 Packaging
中有一个 Surcharge 对象。代码将如下所示;
@ManyToOne
@JoinColumn(name = "CATEGORY", insertable = false, updatable = false)
@JoinColumn(name = "PAYMENT_MEANS", insertable = false, updatable = false)
private Surcharge surcharge;
如果您不想这样,您也可以创建一个 DTO 并使用 JPQL 相应地填充它。
考虑以下数据。这是一个虚构的在线商店系统。 Surcharge
根据不同的商品类别(食物或衣服)和支付方式(现金或信用卡)存储附加费配置。 Packaging
根据商品类别和付款方式存储可用的包装选项。如果您用现金支付食物,抱歉没有优质包装给您。
- 下划线表示主键。
Surcharge
和Packaging
是一对多的关系。我的情况是,我不想拥有 4 个 Surcharge
对象实例,每个实例都有一个 List<Packaging>
。我想要 7 个对象,每个对象都有 category
、paymentMeans
、surcharge
、option
的副本。就像他们加入时一样,结果将是 7 行。如何为我的 java class 建模以实现此目的?谢谢。
@Entity
@Table(name = "SURCHARGE")
public class Surcharge implements Serializable {
@EmbeddedId
private SurchargeId surchargeId;
@Column(name = "SURCHARGE")
private int surcharge;
// how to map this attribute with packaging table as a single instance?
private Packaging packaging;
}
@Embeddable
public class SurchargeId implements Serializable {
@Column(name = "CATEGORY")
private String category;
@Column(name = "PAYMENT_MEANS")
private String paymentMeans;
}
@Entity
@Table(name = "PACKAGING")
public class Packaging implements Serializable {
@EmbeddedId
private PackagingId packagingId;
}
@Embeddable
public class PackagingId implements Serializable {
@Column(name = "CATEGORY")
private String category;
@Column(name = "PAYMENT_MEANS")
private String paymentMeans;
@Column(name = "OPTION")
private String option;
}
在Surcharge
中,总会有Packaging
的列表。你可以做的是在 Packaging
中有一个 Surcharge 对象。代码将如下所示;
@ManyToOne
@JoinColumn(name = "CATEGORY", insertable = false, updatable = false)
@JoinColumn(name = "PAYMENT_MEANS", insertable = false, updatable = false)
private Surcharge surcharge;
如果您不想这样,您也可以创建一个 DTO 并使用 JPQL 相应地填充它。