I have some problems to implement relation OneToOne, ManyToOne, OneToMany in some situation.
Everything happens on a legacy database that I can not change (or can change only slightly)
First problem
Code:
TableA
Field1 PK
Field2 PK
Field3
Field4 UK
Field5 UK
Primary key is Field1+Field2 and it is implemented by EmbeddedId and a PK Class
Another unique key is Field4+Field5 and it is One Side for various Foreign key (OneToMany or even OneToOne).
I have added
Code:
@Table(
name = "TableA",
uniqueConstraints={
@UniqueConstraint(
columnNames={"Field4", "Field5"}
)
}
)
but then i did not understand how i should implement the key.
If i use (on TableB Entity)
Code:
@JoinColumns({
@JoinColumn(name = "Field1", referencedColumnName = "Field4", insertable = false, updatable = false),
@JoinColumn(name = "Field2", referencedColumnName = "Field5", insertable = false, updatable = false)
})
@ManyToOne
private TableA obj;
i obtain "not mapped to single property" error.
It is exactly the problem described on page 336 of the book.
But the book solution is with xml, instead i need solution with annotation (and possibly pure JPA annotation)
Second problemCode:
TableA
Field1 PK, UK1, UK2, UK3
Field2 PK, UK1, UK2, UK3
Field3 PK
Field4 UK2
Field5 UK1, UK3
Field6 UK1
Field7 UK3
Primary Key is Field1+Field2+Field3
Alternative Unique Key1 is Field1+Field2 (non unique part of PK) + Field5+Field6
Alternative Unique Key2 is Field1+Field2 (non unique part of PK) + Field4
Alternative Unique Key3 is Field1+Field2 (non unique part of PK) + Field5+Field7
TableB FK references to TableA PK. Ordinary administration
TableC FK references to TableA UK1
TableD FK references to TableA UK2
TableE FK references to TableA UK3
What is the Entity model of TableA and its Keys ?
Third ProblemCode:
TableA
Field1 PK, UK1, UK2
Field2 PK, UK1, UK2
Field3 PK, UK1
Field4 PK, UK2
Primary Key is Field1+Field2+Field3+Field4 (but Field4 isn't necessary for the uniqueness)
Unique Key1 is Field1+Field2+Field3
Unique Key2 is Field1+Field2+Field4
Table A is a join table used to implement a ManyToMany relation for TableB with itself.
Field1+Field2+Field3 is TableB primary key and Field1+Field2+Field4 is yet table PK (but another record).
Field1+Field2+Field3+Field4 is Primary Key (on TableA) because TableC is on OneToOne relation with TableA using all four fields.
What is the Entity model design ?
Regards
Sorry for my not very good English