Marcos Oto
Marcos Oto

Reputation: 75

JPA 2.1 @ForeignKey in inheritance class

I'm migrating to JPA 2.1 and I would like to replace @org.hibernate.annotations.ForeignKey to something in inheritance class.

In fields, ok:

@ManyToOne
@JoinColumn(name = "any_columm_field_id",
     foreignKey = @javax.persistence.ForeignKey(name = "any_name_field_fk"))

But in inheritance class, how to do it?

Example code:

Super class

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "ANY_SUPER_TABLE_NAME")
public abstract class AnySuperClass { }

Specific class

@Entity
@Table(name = "ANY_SPECIFIC_TABLE_NAME")
// TODO Replace to JPA 2.1
// @org.hibernate.annotations.ForeignKey(name = "any_specific_any_super_fk")
public class AnySpecificClass extends AnySuperClass { }

Any help?

Thank you for your time.

Upvotes: 1

Views: 1710

Answers (1)

Yann Vo
Yann Vo

Reputation: 1971

According to the doc this should do it:

@Entity
@Table(name = "ANY_SPECIFIC_TABLE_NAME")
@PrimaryKeyJoinColumn(foreignKey = @ForeignKey(name = "any_specific_any_super_fk"))
public class AnySpecificClass extends AnySuperClass { }

It's not easy to find the answer though, with the embedded annotation... it's not obvious where you should be using it.

Not sure this will help you a year later...

Upvotes: 3

Related Questions