简体   繁体   English

在休眠状态下使用复合键

[英]using composite key with hibernate

Suppose i want to have a composite key as street, city for purchase order entity. 假设我想为采购订单实体使用复合键作为街道,城市。

Below is how i identify doing it, 以下是我确定这样做的方式,

    @Embeddable
    public class BillingAddress implements Serializable {

        private String street;
        private String city;

        public BillingAddress(){

        }

        public BillingAddress(String street, String city) {
            this.street = street;
            this.city = city;
        }
        //with getters and setters
}



@Entity
@IdClass(BillingAddress.class)
public class PurchaseOrder {

    public PurchaseOrder(BillingAddress billingAddress) {
        street = billingAddress.getStreet();
        city = billingAddress.getCity();

    }

    @Id
    @AttributeOverrides({
            @AttributeOverride(name = "street", column = @Column(name = "STREET")),
            @AttributeOverride(name = "city", column = @Column(name = "CITY")) })
    private String street;
    private String city;
    private String itemName;

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

}

I want to understand what is really @AttributeOverrides annotation do? 我想了解@AttributeOverrides注释的真正含义是什么? Even i change to colomn name to something STREET1 i still see the table created with column name STREET. 即使我将列名称更改为STREET1,我仍然看到使用列名称STREET创建的表。 So what is column = @Column(name = "STREET")) doing here. 那么column = @Column(name =“ STREET”))在这里做什么。

Also instead of constructore taking the BillingAddress i can have it like a field of PurchaseOrder class right like, 另外,我也可以像构造函数那样使用BillingAddress代替Purchaseing类的字段,

 public class PurchaseOrder {
     BillingAddress billingAddress;

}

In this case how this going to change? 在这种情况下,这将如何改变? Do i still need to have private String street; 我还需要私家String街吗? private String city; 私人弦城市; in PurchaseOrder? 在PurchaseOrder中?

Finally i read that using composite keys should be avoided in new data base system design which using composite primary key is applicable a situation where in order to map the legacy data base tables with out changing the data base table structure right? 最后,我读到在新的数据库系统设计中应避免使用复合键,而在不更改数据库表结构的情况下映射旧数据库表的情况下,使用复合主键是适用的情况吗? Is that statement a valid one? 那句话是有效的吗?

//Edit question //编辑问题

Saving purchase order which billing address is in the field, 保存帐单地址在字段中的采购订单,

PurchaseOrder purchaseOrder = new PurchaseOrder();
purchaseOrder.setItemName("name");

BillingAddress billingAddress = new BillingAddress();
billingAddress.setCity("c1"); billingAddress.setStreet("s1");                                                              purchaseOrder.setBillingAddress(billingAddress); 
session.save(purchaseOrder);

There's are few question you asked, I tried to go through all of them and answer each one: 您提出的问题很少,我尝试遍历所有问题并回答每个问题:

What does @AnnotationOverride do? @AnnotationOverride有什么作用?

answer here: What does @AttributeOverride mean? 在这里回答: @AttributeOverride是什么意思?

The second question is a bit unclear to me but I presume you're asking whether you have to include all the fields from the composite key in the PurchaseOrder class. 第二个问题对我来说还不太清楚,但我想您是在问是否必须将复合键中的所有字段都包含在PurchaseOrder类中。

No, I don't think so. 不,我不这么认为。 Here's an example I've put together real fast: 这是我快速整理的示例:

@Entity
@Table(name = "PURCHASE_ORDER")
public class PurchaseOrder{

    @Id
    private BillingAddress billingAddress;

    //getters & setters

    @Embeddable
    public static class BillingAddress implements Serializable {
        @Column(name = "street")
        private String street;
        @Column(name = "city")
        private String city;
        @Column(name = "itemName")
        private String itemName;

        //getters & setters

    }
}

Don't worry about the syntax, just the structure. 不用担心语法,只担心结构。 You can even add extra field into PurchaseOrder which isn't an id. 您甚至可以将不是ID的多余字段添加到PurchaseOrder中。

Should I use composite keys or not? 是否应该使用组合键?

answer here: Should I use composite primary keys or not? 在这里回答: 是否应该使用复合主键?

Well, your PurchaseOrder class does not extend from a mapped entity of any kind, and neither do the properties that you are (currently) applying the @AttributeOverride s to. 好吧,您的PurchaseOrder类不会从任何类型的映射实体扩展,您(当前)将@AttributeOverride应用于的属性也不会。 So, there is nothing to actually override and your JPA provider is simply ignoring the annotations. 因此,没有什么要实际覆盖的,您的JPA提供程序只是忽略了注释。 What I think you are trying to do is define an embedded id for an entity, while overriding some of the column mappings for that id. 我认为您要尝试的是为实体定义嵌入式ID,同时覆盖该ID的某些列映射。 You can do this with some modifications to your current code: 您可以通过对当前代码进行一些修改来做到这一点:

@Entity
public class PurchaseOrder {
    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "street", column = @Column(name = "BILLING_STREET")),
            @AttributeOverride(name = "city", column = @Column(name = "BILLING_CITY")) })
    private BillingAddress billingAddress;

    private String itemName;

    // Constructors, Getters/Setters
}

Note that I've changed the names of the overridden attributes, since with your current example, the embedded id name and overridden names are the same. 请注意,我更改了覆盖属性的名称,因为在当前示例中,嵌入的id名称和覆盖的名称是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM