I have the following Java classes with JPA 2.0 annotations:
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
}
Also classes B and C which are very similar.
Next I have Class AB which has a compound PK (A and B) with FK's to A and B:
@Entity
public class AB {
@Id @ManyToOne private A a;
@Id @ManyToOne private B b;
}
Next I have class AC which is similar to AB:
@Entity
public class AC {
@Id @ManyToOne private A a;
@Id @ManyToOne private C c;
}
So far all the database constraints are exactly as I need them. Now the next part which I cannot quite figure out:
I have class X. It should look like this:
public class X {
private A a;
private B b;
private C c;
}
Here are the requirements: A and B should be a compound Primary Key and also a compound Foreign Key to AB.
A and C should be a compound Foreign Key to AC.
I cannot think of the correct annotations to make this work. Any suggestions?
EDIT:
OK then. I have manually created all tables, including table X wit this DDL:
create table X (
A_ID int not null,
B_ID int not null,
C_ID int not null,
primary key (A_ID, B_ID),
constraint `fk_ab` foreign key (A_ID, B_ID) references AB (A_ID, B_ID),
constraint `fk_ac` foreign key (A_ID, C_ID) references AC (A_ID, C_ID)
) engine=InnoDB;
and run the Hibernate Reverse Engineering tool on it. I get an error:
org.hibernate.MappingException: An association from the table X refers to an unmapped class: Ac
For now, I'm totally blocked, so I'm shelving my plan to use JPA and I'm using MyBatis instead. I think I have a schema which is not possible to represent with JPA or Hibernate. Unless anyone can prove me wrong?