gpt4 book ai didi

java - Hibernate 中的三元(和 n 元)关系

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:57:31 25 4
gpt4 key购买 nike

问题 1) 我们如何使用 Hibernate 对三元关系建模?例如,我们如何对呈现的三元关系建模 here使用 Hibernate(或 JPA)?

注意:我知道 JPA 2 添加了一些使用映射构建三元关系的结构。但是,这个问题假定 JPA 1 或 Hibernate 3.3.x,我不喜欢使用映射来对此建模。

ER Model
(来源:grussell.org)


ER Model with ternary relationships replaced
(来源:grussell.org)

理想情况下,我希望我的模型是这样的:

class SaleAssistant {
Long id;
//...
}

class Customer {
Long id;
//...
}

class Product {
Long id;
//...
}

class Sale {
SalesAssistant soldBy;
Customer buyer;
Product product;
//...
}

问题 1.1)

我们如何为这种变化建模,其中每个销售项目可能有许多产品?

class SaleAssistant {
Long id;
//...
}

class Customer {
Long id;
//...
}

class Product {
Long id;
//...
}

class Sale {
SalesAssistant soldBy;
Customer buyer;
Set<Product> products;
//...
}

问题 2)一般来说,我们如何使用 Hibernate 对 n 元、n >= 3 的关系进行建模?

提前致谢。

最佳答案

Q1. How can we model a ternary relationship using Hibernate? For example, how can we model the ternary relationship presented here using Hibernate (or JPA)? (...)

我会重构与中间实体类的关联(这是 Hibernate 推荐的方式)。应用于您的示例:

@Entity
public class Sale {
@Embeddable
public static class Pk implements Serializable {
@Column(nullable = false, updatable = false)
private Long soldById;

@Column(nullable = false, updatable = false)
private Long buyerId;

@Column(nullable = false, updatable = false)
private Long productId;

public Pk() {}

public Pk(Long soldById, Long buyerId, Long productId) { ... }

// getters, setters, equals, hashCode
}

@EmbeddedId
private Pk pk;

@ManyToOne
@JoinColumn(name = "SOLDBYID", insertable = false, updatable = false)
private SaleAssistant soldBy;
@ManyToOne
@JoinColumn(name = "BUYERID", insertable = false, updatable = false)
private Customer buyer;
@ManyToOne
@JoinColumn(name = "PRODUCTID", insertable = false, updatable = false)
private Product product;

// getters, setters, equals, hashCode
}

Q1.1. How can we model this variation, in which each Sale item might have many Products?

我不会在这里使用复合主键并为 Sale 实体引入 PK。

Q2. In general, how can we model n-ary, n >= 3 relationships with Hibernate?

我认为这是我对 Q1 的回答。包括这个。如果不是,请澄清。


更新: 回复 OP 的评论

(...) the pk's fields are not getting populated and as a result I cannot save Sale items in the DB. Should I use setters like this for the Sale class? public void setBuyer(Customer cust) { this.buyer = cust; this.pk.buyerId = cust.getId(); }

您需要创建一个新的 Pk(为简洁起见,我从原始答案中删除了构造函数)并将其设置在 Sale 项目上。我会做这样的事情:

Sale sale = new Sale();
Pk pk = new Pk(saleAssistant.getId(), customer.getId(), product.getId());
sale.setPk(pk);
sale.setSoldBy(saleAssistant);
sale.setBuyer(customer);
sale.setProduct(product);
...

然后坚持销售

Also, in the JoinColumn annotations, what column are "name" fields referring to? The target relations' pks or the sale table's own column names?

对于复合 Pk 的属性列(即销售表自己的列名),我们希望它们获得 PK 和 FK 约束。

关于java - Hibernate 中的三元(和 n 元)关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2613347/

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