0

I want to use mixed @Inheritance strategy, but Hibernate doesn't support it.
Is there any way to implement JOINED inheritance without actual class inheritance. For example:

@Entity
@Table(name="A")
@Inheritance(strategy=InheritanceType.JOINED)
public class A { 
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQ")
  private Long id;

  //getters
  //setters
}

@Entity
@Table(name="B")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class B {
  @Id
  private Long id;

  //getters
  //setters
}

So, basically in B I just want to refer to @Id generated in A without extending from A.

jFrenetic
  • 5,384
  • 5
  • 42
  • 67

2 Answers2

1

I found the solution. JPA doesn't allow you to combine @Id and @OneToOne. However, @MapsId annotation does the trick:

@Entity
public class A { 
  @Id
  private Long id;

  //getters
  //setters
}

@Entity
public class B {
  @Id
  private Long id;

  @MapsId
  @OneToOne(optional=false, fetch=FetchType.EAGER)
  @JoinColumn(nullable=false, name="id")
  private A a;

  //getters
  //setters
}
jFrenetic
  • 5,384
  • 5
  • 42
  • 67
0

I think you can accomplish this by making a @OneToOne relationship or a @OneToMany and point the table name like this

  @Id @OneToOne
  @JoinColumn(name = "id")
  private A a;
Cristiano Fontes
  • 4,920
  • 6
  • 43
  • 76
  • Thanks for the answer. But unfortunately as you can see [here](http://stackoverflow.com/questions/787698/jpa-hibernate-one-to-one-relationship) the Id annotation cannot be combined with OneToOne. – jFrenetic Oct 07 '11 at 19:04
  • I found the solution. Check it out, if you're interested. – jFrenetic Oct 10 '11 at 18:32