I’ve never seen something like this before
EntityA
@Id
long id
@Column(name ="field1")
String field1
@Column(name ="field2")
String field2
@IdClass(compositePK.class)
EntityB
@Id
String field1
@Id
String field2
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
EntityA myEntityA
Ok all is ok the tableB need to have the EntityA as foreign key but defined on the id not on field1,field2.
I get error like
Foreign key (FK74D979395D87E41E:EntityA [id])) must have same number of columns as the referenced primary key (EntityA [field1,field2])
I understand that hibernate required that the FK need to be the same as the PK but I can’t made this the entityB PK must stay field1,field2 I can’t change EntityA for the moment.
By what I understand from your code, you’ve got
EntityAwith simple PKidandEntityBwith a composite PKCompositePK. For a@OneToOneassociation to work, both entities need to share a primary key/have the same PK class (or at least a unique constraint simulating the PK)It is not possible to use this kind of mapping with this structure, you should either:
EntityAandEntityBshare their PK, or there’s a FK fromEntityBtoEntityA.OneToManyinstead, even if it will always contain ‘one’ in the ‘many’ side.See this section on Hibernate Reference for some background: 2.2.5. Mapping entity associations/relationships – One to one, specially the second part talking about FK’s: