tags:

views:

360

answers:

2
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Foo   

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class BarFoo extends Foo

mysql> desc foo;
+---------------+-------------+
| Field         | Type        |
+---------------+-------------+
| id            | int         |
+---------------+-------------+

mysql> desc barfoo;
+---------------+-------------+
| Field         | Type        |
+---------------+-------------+
| id            | int         |
| foo_id        | int         |
| bar_id        | int         |
+---------------+-------------+

mysql> desc bar;
+---------------+-------------+
| Field         | Type        |
+---------------+-------------+
| id            | int         |
+---------------+-------------+

Is it possible to specify column barfo.foo_id as the joined column?

Are you allowed to specify barfoo.id as BarFoo's @Id since you are overriding the getter/seeter of class Foo?

I understand the schematics behind this relationship (or at least I think I do) and I'm ok with them.

The reason I want an explicit id field for BarFoo is exactly because I want to avoid using a joined key (foo _id, bar _id) when querying for BarFoo(s) or when used in a "stronger" constraint. (as Ruben put it)

+1  A: 

When InheritanceType.JOINED is defined the @Id column will automatically be used for joining. There is only one id value for a Barfoo instance, and this is persisted in the id column of both the foo and the barfoo table.

There is no need to have separate id and foo_id columns in the barfoo table. Nor is it necessary by default to overwrite the id getter and setter in barfoo. You would only overwrite the id setter and getter if the id of a Barfoo instance has stronger constraints than the id of a Foo instance.

Ruben
A: 

Check out @PrimaryKeyJoinColumn

@PrimaryKeyJoinColumns({ @PrimaryKeyJoinColumn(name="CHILD_PK1", referencedColumnName="PARENT_PK1"), @PrimaryKeyJoinColumn(name="CHILD_PK2", referencedColumnName="PARENT_PK2") }) public class Child extends Parent { ... }

Razola