What is the JPA equivalent to the Hibernate Identity Generator?

What is the JPA equivalent to the Hibernate Foreign Identity Generator?

<id column="PERSON_ID" name="id" type="java.lang.Long">
   <generator class="foreign">
      <param name="property">person</param>
   </generator>
</id>

      

+3


source to share


2 answers


AFAIK spec, JPA does not standardize external identity generator. You must programmatically set the PK value correctly before saving this instance.

As far as Hibernate is concerned, it has an extension annotation to include the foreign generator ID . You can use it if you don't mind:



  @Id
  @GeneratedValue(generator = "myForeignGenerator")
  @org.hibernate.annotations.GenericGenerator(
        name = "myForeignGenerator",
        strategy = "foreign",
        parameters = @Parameter(name = "property", value = "person")
  )
  @Column(name = "PERSON_ID")
  private Long id;

      

+6


source


For what it's worth JPA 2.0 adds a @MappedBy annotation that can be used to import a foreign key. Starting with a Christian example and a little bored by Ken Chen:

@Id
@Column
private Long personId;

@ManyToOne
@JoinColumn(name = "personId")
@MapsId
private Person person;

      



I know this question has been around for quite some time, but since I stumbled upon it while solving the same problem and then dug it out @MappedBy

, I thought I'd add it for anyone else who comes across this later.

0


source







All Articles