Có ai biết nếu nó có thể thiết lập một backreference từ bên trong một JPA @EmbeddedId
.Làm thế nào để thiết lập một backreference từ một @EmbeddedId trong JPA
Vì vậy, ví dụ có một Entity của Mẫu
@Entity
public class Entity1 {
@Id
@GeneratedValue
private String identifier;
private Entity1 relationToEntity1;
//Left out the getters and setters for simplicity
}
Và một thực thể thứ hai với một Id nhúng phức tạp. Một phần của thực thể thứ hai này là một tham chiếu đến thực thể cha của nó. Cũng giống như vậy:
@Entity
public class Entity2 {
@EmbeddedId private Entity2Identifier id;
//Left out the getters and setters for simplicity.
}
@Embedabble
public class Entity2Identifier {
private String firstPartOfIdentifier;
private Entity1 parent;
//Left out the getters and setters for simplicity.
}
Khi tôi cố gắng tiết kiệm một cấu trúc như vậy qua JPA (thực hiện là EclipseLink) vào một cơ sở dữ liệu tôi nhận được một vài trường hợp ngoại lệ có dạng:
Exception [EclipseLink-93] (Eclipse Persistence Services - 1.1.0.r3639-SNAPSHOT):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [ENTITY1] is not present in this descriptor.
Descriptor: RelationalDescriptor(test.Entity2 --> [DatabaseTable(ENTITY2)])
Có ai đó gặp sự cố như vậy và có giải pháp không?
OK. Tôi đoán ví dụ của tôi là đơn giản. Cha mẹ thực sự có một ID nhúng phức tạp, bao gồm hai chuỗi, là tốt. Đó là lý do tại sao tôi không thể chỉ đơn giản là tham chiếu ID của nó. – ali
Điều đó cũng đơn giản. Tôi đã cập nhật ví dụ ở trên để sử dụng EmbeddedId trên thực thể 1. –
@MapsId là người chiến thắng! Rất hữu ích. –