简体   繁体   English

Hibernate复合键id生成器

[英]Hibernate composite key id generator

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 我想为orderId自动生成/使用一些生成器

Yes I have read below. 是的我已在下面阅读。 http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier

I donot want to manage the id generation process as above recommends application generating the orderId. 我不想管理id生成过程,因为上面建议应用程序生成orderId。

How to make the partial id generator work.. what are my options..would greatly appreciate some thoughts by experts. 如何使部分id生成器工作..我的选择是什么...将非常感谢专家的一些想法。

@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;
}

}

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 (ie composite PKs). 我一直徘徊在万维网上的所有可能的链接上,并试图找到为什么你不能将@GeneratedValue与@EmbeddedId或@IdClass(即复合PK)一起使用。 The reason is that you just CANNOT. 原因是你不能。 An explanation provided here might help you feel a bit better: JAVA.NET/GLASSFISH 这里提供的解释可能会帮助您感觉更好: JAVA.NET / GLASSFISH

Composite PKs are ASSIGNMENT-based not GENERATION-based. 复合PK是基于ASSIGNMENT的而不是基于GENERATION的。 Therefore, any @GeneratedValue stuff are not supposed to work with them. 因此,任何@GeneratedValue内容都不适用于它们。 I am also having problem in my project and I think there is no other way, EXCEPT: 我的项目也有问题,我认为除此之外别无他法:

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 (ie persistence object collections). 如果您知道您的@GeneratedValue ID在您的域(例如,您的数据库)的上下文中始终是唯一的,则您不需要使用复合PK并进行一些内部检查以确定记录的唯一性(即持久性对象集合) 。

I think this question of stackoverflow might help you. 我认为stackoverflow的这个问题可能会对你有所帮助。 Although it was asked for different purpose, it has the answer you might need. 虽然它被要求用于不同的目的,但它有你可能需要的答案。 It has used both composite primary key and generation type. 它使用了复合主键和生成类型。

Isn't using @IdClass a solution? 是不是使用@IdClass解决方案? One can use @Id on each of the columns that form the primary key and @GeneratedValue and @SequenceGenerator on the @Id column that should be generated by a sequence. 可以在构成主键的每个列上使用@Id,在@Id列上使用@GeneratedValue和@SequenceGenerator,它们应该由序列生成。

Generation strategy of composite primary key can be managed by @IdClass. 复合主键的生成策略可以由@IdClass管理。 Annotate the class with @IdClass for which you need composite pk. 使用@IdClass为您需要复合pk的类注释。 Annotate the fields that make for composite pk with @Id and @GeneratedValue. 使用@Id和@GeneratedValue注释复合pk的字段。 This works. 这有效。 eg 例如

@Entity
@IdClass(B.class)
Class A {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int i;
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int j;

    private String variable;
}

Class B implements Serializable{
    private int i;
    private int j;

    public int geti(){return i;}
    public int getj(){return j;}
}

In this example, combination of i and j will work as composite primary key for A. Their values are auto generated. 在此示例中,i和j的组合将作为A的复合主键。它们的值是自动生成的。

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

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