Tôi có hai bảng này trên cơ sở dữ liệu revisions
và Pagu
Hibernate 3 composite một chìa khóa với GeneratedValue
trong mô hình Pagu
, tôi phải chính tổng hợp:
- id int (autogenerated bởi cơ sở dữ liệu) Bảng
- revision_id (ngoại trừ bản chỉnh sửa)
cách triển khai tính năng này trên Hibernate 3?
đây là những gì tôi đã đưa ra
@Entity
@Table(name="pagu"
,schema="dbo"
,catalog="dbname"
)
@IdClass(PaguId.class)
public class Pagu implements java.io.Serializable {
private int id;
private int revisiId;
private Entitas entitas;
private Revisi revisi;
...
@Id
@GeneratedValue
@Column(name="id", unique=true, nullable=false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Id
@Column(name="revisi_id", unique=true, nullable=false)
public int getRevisiId() {
return this.revisiId;
}
public void setRevisiId(int id) {
this.id = id;
}
Và đây là lớp PaguId tôi
@Embeddable
public class PaguId implements java.io.Serializable {
private int id;
private int revisiId;
public PaguId() {
}
public PaguId(int id, int revisiId) {
this.id = id;
this.revisiId = revisiId;
}
@Column(name="id", nullable=false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="revisi_id", nullable=false)
public int getRevisiId() {
return this.revisiId;
}
public void setRevisiId(int revisiId) {
this.revisiId = revisiId;
}
public boolean equals(Object other) {
if ((this == other)) return true;
if ((other == null)) return false;
if (!(other instanceof PaguId)) return false;
PaguId castOther = (PaguId) other;
return (this.getId()==castOther.getId() && this.getRevisiId()==castOther.getRevisiId())
&& (this.getRevisiId()==castOther.getRevisiId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getId();
result = 37 * result + this.getRevisiId();
return result;
}
}
Khi tôi cố gắng tiết kiệm này trên cơ sở dữ liệu tôi đã nhận lỗi:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
- CẬP NHẬT-- Nhưng thay đổi triển khai bằng cách sử dụng EmbeddedId như thế này
public class Pagu implements java.io.Serializable {
private PaguId id;
...
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="id", [email protected](name="id", nullable=false)),
@AttributeOverride(name="revisiId", [email protected](name="revisi_id", nullable=false)) })
public PaguId getId() {
return this.id;
}
public void setId(PaguId id) {
this.id = id;
}
....
Nó được biên dịch đúng, nhưng đã cho tôi lỗi khi duy trì mô hình.
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): id.model.Pagu
Bạn không nên sử dụng '@ Embeddable' và' @ IdClass' cùng một lúc. Chọn sử dụng: 1. '@ Embeddable' trên lớp khóa chính hỗn hợp và' @ EmbeddedId' trong lớp thực thể hoặc 2. không có chú thích trên lớp khóa chính hỗn hợp và '@ IdClass' ở cấp lớp thực thể với nhiều Chú thích '@ Id' trong chính lớp thực thể (đối với các trường). –
Làm điều này cũng tạo ra ngoại lệ 'một đối tượng khác có cùng giá trị số nhận dạng đã được liên kết với phiên: [id.go.model.Pagu # id.go..model.PaguId @ 5ae9]' lỗi loại tôi nhận được nếu có không phải là @GeneratedValue trên @id – ahmy
Tôi đã tìm thấy liên kết để tạo một số định danh một phần http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1150 nhưng cũng có cảnh báo rằng cấu trúc như vậy về cơ bản sai – ahmy