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

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

问题描述

Q 1)我们如何使用Hibernate建立三元关系?例如,我们如何使用Hibernate(或JPA)为 here 建立三元关系, ?



注意:我知道JPA 2添加了一些使用地图构建三元关系的构造。但是,这个问题假设JPA 1或Hibernate 3.3.x,我不喜欢使用地图建模。



ER模型http://db.grussell.org/co22001%20notes_files/image043.gif






ER模型与三元关系取代http://db.grussell.org/co22001%20notes_files/image045.gif



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

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

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

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

class Sale {
SalesAssistant soldBy;
客户买家;
产品;
// ...
}

Q 1.1)



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

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

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

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

class Sale {
SalesAssistant soldBy;
客户买家;
设置<产品>产品;
// ...
}

Q 2)一般来说,我们可以用Hibernate来建模n-ary,n> = 3关系吗?



提前感谢

解决方案


Q1。我们如何使用Hibernate建立三元关系?例如,我们如何使用Hibernate(或JPA)来模拟这里呈现的三元关系? (...)


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

  @Entity 
public class Sale {
@Embeddable
public静态类Pk实现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)
私人客户买家;
@ManyToOne
@JoinColumn(name =PRODUCTID,insertable = false,updatable = false)
private产品;

// getter,setters,equals,hashCode
}




Q1.1。我们如何模拟这个变体,其中每个销售项目可能有很多产品?


我不会使用复合主键并为销售实体引入PK。


Q2。一般来说,我们如何用Hibernate来模拟n-ary,n> = 3的关系?


我认为我对Q1的回答。涵盖这一点。如果没有,请澄清。






更新: / p>


(...)pk的字段未被填充,因此我无法在DB中保存销售项目。我应该用这种销售类的设置器吗? public void setBuyer(Customer cust){this.buyer = cust; this.pk.buyerId = cust.getId();您需要创建一个新的 Pk (我从中删除了构造函数我的原始答案简洁),并将其设置在销售项目。我会这样做:

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

然后坚持销售


此外,在JoinColumn注释中,哪些列是名称字段引用?目标关系'pks或销售表自己的列名称


对于复合属性的列 Pk (即销售表自己的列名称),我们希望他们获得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.

ER Model http://db.grussell.org/co22001%20notes_files/image043.gif


ER Model with ternary relationships replaced http://db.grussell.org/co22001%20notes_files/image045.gif

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-ary)关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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