Problem
I would like to start by saying that I realize the database structure is atrocious, but I cannot change it at this point in time.
That being said, I have the need to create a one-to-many, bi-directional relationship in Hibernate (4.2.1) which involves no primary keys (only a unique key on the "parent" side of the relationship) and no join tables. The foreign key representing this relationship is a backpointer from the "child" to the "parent" (see below). I have searched and tried various different annotation configurations with no luck. Is what I'm asking for possible?
Database
GLOBAL_PART
CREATE TABLE "GLOBAL_PART" (
"GLOBAL_PART_ID" NUMBER NOT NULL,
"RELEASES" NUMBER,
CONSTRAINT "GLOBAL_PART_PK" PRIMARY KEY ("GLOBAL_PART_ID"),
CONSTRAINT "GLOBAL_PART_RELEASES_UK" UNIQUE ("RELEASES")
);
PART_RELEASE
CREATE TABLE "PART_RELEASE" (
"PART_RELEASE_ID" NUMBER NOT NULL,
"COLLECTION_ID" NUMBER,
CONSTRAINT "PART_RELEASE_PK" PRIMARY KEY ("PART_RELEASE_ID"),
CONSTRAINT "GLOBAL_PART_RELEASE_FK" FOREIGN KEY ("COLLECTION_ID")
REFERENCES "GLOBAL_PART" ("RELEASES") ON DELETE CASCADE ENABLE
);
Reference:
PART_RELEASE GLOBAL_PART
------------------- ------------
PART_RELEASE_ID (PK) GLOBAL_PART_ID (PK)
COLLECTION_ID -------------> RELEASES (UK)
Java
GlobalPart.java
@Entity
@Table(name = "GLOBAL_PART")
@SequenceGenerator(name = "SEQUENCE_GENERATOR", sequenceName = "GLOBAL_PART_SEQ")
public class GlobalPart extends ModelBase implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "GLOBAL_PART_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_GENERATOR")
private Long globalPartId;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "COLLECTIONGID")
private Set<PartRelease> releases;
public Long getGlobalPartId() {
return globalPartId;
}
public void setGlobalPartId(Long globalPartId) {
this.globalPartId = globalPartId;
}
public Set<PartRelease> getReleases() {
return releases;
}
public void setReleases(Set<PartRelease> releases) {
this.releases = releases;
}
}
PartRelease.java
@Entity
@Table(name = "PART_RELEASE")
@SequenceGenerator(name = "SEQUENCE_GENERATOR", sequenceName = "PART_RELEASE_SEQ")
public class PartRelease extends ModelBase implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "PART_RELEASE_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQUENCE_GENERATOR")
private String partReleaseId;
@ManyToOne
@JoinColumn(name = "RELEASES")
private GlobalPart globalPart;
public String getPartReleaseId() {
return partReleaseId;
}
public void setPartReleaseId(String partReleaseId) {
this.partReleaseId = partReleaseId;
}
public GlobalPart getGlobalPart() {
return globalPart;
}
public void setGlobalPart(GlobalPart globalPart) {
this.globalPart = globalPart;
}
}
Any help would be greatly appreciated!!
releaseNumber
property mapped to theRELEASES
column. Eventually I convinced myself I was doing it wrong (by misreading MappingException) and took the approach mentioned in the question. Thanks, again! – Issacissachar