Mock JOINED inheritance strategy without actual inheritance
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")
@Inheri开发者_Go百科tance(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
.
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
}
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;
精彩评论