Hibernate 中的三元(和 n 元)关系 [英] Ternary (and n-ary) relationships in Hibernate

查看:25
本文介绍了Hibernate 中的三元(和 n 元)关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Q 1) 我们如何使用 Hibernate 建模三元关系?例如,我们如何使用 Hibernate(或 JPA)对
(来源:
(来源:grussell.org)

理想情况下,我更喜欢我的模型是这样的:

class SaleAssistant {长ID;//...}类客户{长ID;//...}类产品{长ID;//...}类销售{销售助理soldBy;客户买家;产品产品;//...}

Q 1.1)

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

class SaleAssistant {长ID;//...}类客户{长ID;//...}类产品{长ID;//...}类销售{销售助理soldBy;客户买家;设置<产品>产品;//...}

Q 2) 一般来说,我们如何用 Hibernate 建模 n 元、n >= 3 的关系?

提前致谢.

解决方案

第一季度.我们如何使用 Hibernate 建模三元关系?例如,我们如何使用 Hibernate(或 JPA)对此处呈现的三元关系进行建模?(...)

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

@Entity公开课销售{@Embeddable公共静态类 Pk 实现了 Serializable {@Column(可为空 = 假,可更新 = 假)私人 Long sellById;@Column(可为空 = 假,可更新 = 假)私人多头买家ID;@Column(可为空 = 假,可更新 = 假)私人长产品ID;公共 Pk() {}public Pk(Long sellById, LongbuyerId, Long productId) { ... }//getter、setter、equals、hashCode}@EmbeddedId私人pk;@ManyToOne@JoinColumn(name = "SOLDBYID",可插入 = 假,可更新 = 假)私人销售助理soldBy;@ManyToOne@JoinColumn(name = "BUYERID", insertable = false, updatable = false)私人客户买家;@ManyToOne@JoinColumn(name = "PRODUCTID", insertable = false, updatable = false)私人产品产品;//getter、setter、equals、hashCode}

<块引用>

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

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

<块引用>

第 2 季度.一般来说,我们如何用 Hibernate 建模 n-ary, n >= 3 关系?

我认为我对 Q1 的回答.涵盖这个.如果没有,请澄清.

<小时>

更新:回复来自 OP 的评论

<块引用>

(...) pk 的字段没有被填充,因此我无法将销售项目保存在数据库中.我应该在 Sale 类中使用这样的 setter 吗?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(客户);sale.setProduct(产品);...

然后坚持sale.

<块引用>

此外,在 JoinColumn 注释中,名称"字段指的是什么列?目标关系的pks还是sale表自己的列名?

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

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

NOTE: I know that JPA 2 has added some constructs for building ternary relationships using maps. However, this question assumes JPA 1 or Hibernate 3.3.x and I don't like to use maps to model this.


(source: grussell.org)



(source: grussell.org)

Ideally I prefer my model to be like this:

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

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

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

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

Q 1.1)

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

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

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

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

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

Q 2) In general, how can we model n-ary, n >= 3 relationships with Hibernate?

Thanks in advance.

解决方案

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)? (...)

I would remodel the association with an intermediate entity class (and that's the recommended way with Hibernate). Applied to your example:

@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?

I wouldn't use a composite primary key here and introduce a PK for the Sale entity.

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

I think that my answer to Q1. covers this. If it doesn't, please clarify.


Update: Answering comments from the 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(); }

You need to create a new Pk (I removed the constructors from my original answer for conciseness) and to set it on the Sale item. I would do something like this:

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);
...

And then persist the sale.

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

To the columns for the attributes of the composite Pk (i.e. the sale table's own column names), we want them to get PK and FK constraints.

这篇关于Hibernate 中的三元(和 n 元)关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆