我的实体如下。我的数据模型在下面强制执行,因此我无法更改引用的完整性。因此,我陷入了复合键的困境。我想自动生成/使用一些生成器作为orderId
是的,我已经阅读以下内容。 http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity- mapping- identifier
我不想管理id生成过程,因为上面建议应用程序生成orderId。
如何使部分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; } }
我一直将鼠标悬停在万维网站点上的所有可能链接上,试图找到为什么不能将@GeneratedValue与@EmbeddedId或@IdClass(即复合PK)一起使用的原因。原因是您不能。这里提供的解释可能会帮助您感觉更好: JAVA.NET/GLASSFISH
复合PK基于分配而不是基于生成。因此,任何@GeneratedValue东西都不应该与它们一起使用。我的项目也遇到了问题,我认为没有别的办法,除了:
如果您知道@GeneratedValue ID在域(例如,数据库)的上下文中始终是唯一的,则无需使用复合PK并进行一些内部检查以确定记录的唯一性(即持久性对象集合) 。