0

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?

NickJ
  • 9,380
  • 9
  • 51
  • 74
  • Since, you've created class `AB` why not have instance variable of `AB` in `X` instead of both `A` and `B`? – TheKojuEffect Oct 03 '13 at 15:14
  • If I have an instance of AB in X, then logically I'd also have an instance of AC (my other foreign key), but then there'd be no way of ensuring that AB and AC both have the same A. – NickJ Oct 03 '13 at 15:18
  • Anyway, You try out `@IdClass` annotation. Hope that will help. – TheKojuEffect Oct 03 '13 at 15:19
  • EmbeddedId is what you're looking for: http://docs.oracle.com/javaee/6/api/javax/persistence/EmbeddedId.html –  Oct 03 '13 at 12:42

0 Answers0