Hibernate组合键ID生成器 [英] Hibernate composite key id generator

查看:93
本文介绍了Hibernate组合键ID生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的实体如下。
我的数据模型在下面强制执行,我无法更改参考迭代。
所以我坚持使用复合键。
我想自动生成/为orderId使用一些生成器

I have my entities as below. My data model enforces below and I cannot change referential itegrity. So I am stuck with a composite key. I want to autogenerate/use some generator for orderId

是的,我已阅读下面。
http:// docs。 jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier

Yes I have read below. http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier

我不想管理id生成过程如上所述,建议应用程序生成orderId。

I donot want to manage the id generation process as above recommends application generating the orderId.

如何使部分ID生成器工作..我的选择是什么..非常感谢专家的一些想法。

How to make the partial id generator work.. what are my options..would greatly appreciate some thoughts by experts.

@Entity
@Table(name = "Orders", uniqueConstraints = @UniqueConstraint(columnNames = {"partner_ID", "order_ident" }))
public class Order  {

private OrderId id;

public Order() {
}


@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "partnerId", column = @Column(name = "partner_ID", nullable = false)),
        @AttributeOverride(name = "employeeId", column = @Column(name = "employee_ID", nullable = false)),
        @AttributeOverride(name = "orderId", column = @Column(name = "order_ID", nullable = false)) })
public OrderId getId() {
    return this.id;
}

public void setId(OrderId id) {
    this.id = id;
}


}


@Embeddable
public class OrderId extends FactObject {

private int partnerId;
private int employeeId;
private int orderId;

public OrderId() {
}

public OrderId(int partnerId, int employeeId, int orderId) {
    this.partnerId = partnerId;
    this.employeeId = employeeId;
    this.orderId = orderId;
}

@Column(name = "partner_ID", nullable = false)
public int getpartnerId() {
    return this.partnerId;
}

public void setpartnerId(int partnerId) {
    this.partnerId = partnerId;
}

@Column(name = "employee_ID", nullable = false)
public int getemployeeId() {
    return this.employeeId;
}

public void setemployeeId(int employeeId) {
    this.employeeId = employeeId;
}

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
@Column(name = "order_ID",insertable=false, nullable=false,  updatable=false)
public int getOrderId() {
    return this.orderId;
}

public void setOrderId(int orderId) {
    this.orderId = orderId;
}

public boolean equals(Object other) {
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof OrderId))
        return false;
    OrderId castOther = (OrderId) other;

    return (this.getpartnerId() == castOther.getpartnerId())
            && (this.getemployeeId() == castOther.getemployeeId())
            && (this.getOrderId() == castOther.getOrderId());
}

public int hashCode() {
    int result = 17;

    result = 37 * result + this.getpartnerId();
    result = 37 * result + this.getemployeeId();
    result = 37 * result + this.getOrderId();
    return result;
}

}


推荐答案

我一直徘徊在万维网上所有可能的链接上,并试图找出为什么你不能在@EmbeddedId或@IdClass(即复合PK)中使用@GeneratedValue。原因是你只是不能。这里提供的解释可能会帮助您感觉更好一些: JAVA.NET/GLASSFISH

I have been hovering over all the possible links on World Wide Websites and trying to find why you cannot use @GeneratedValue with @EmbeddedId or @IdClass (i.e. composite PKs). The reason is that you just CANNOT. An explanation provided here might help you feel a bit better: JAVA.NET/GLASSFISH

复合PK是基于指派而非基于GENERATION的。因此,任何@GeneratedValue的东西都不应该与他们一起工作。我在我的项目中也遇到了问题,我认为除此之外别无他法:

Composite PKs are ASSIGNMENT-based not GENERATION-based. Therefore, any @GeneratedValue stuff are not supposed to work with them. I am also having problem in my project and I think there is no other way, EXCEPT:


如果您知道您的@GeneratedValue ID是在你的域的上下文中(例如你的数据库)永远是唯一的,你不需要使用组合PK并且有一些内部检查来确定你的记录的唯一性(即持久化对象集合)。

If you know that your @GeneratedValue ID is always unique in context of your domain (for example, your database) you don't need to use composite PK and have some internal checks to determine the uniqueness of your records (i.e. persistence object collections).

这篇关于Hibernate组合键ID生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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