Hibernate 5 and using @GeneratedValue inside @Embeddable annotated objects used as composite primary keys

I’m migrating JPA entities from OpenJPA 2.2.x (JPA 2.0) to Hibernate 5.2 (JPA 2.1) and I’ve noticed a peculiar problem. Many of my generated ids are not being populated.
Many of my entities contain composite keys contained in classes annotated as Embeddable. These classes contain attributes annotated with Column, GeneratedValue, and SequenceGenerator.

PLEASE NOTE: I’m really trying to avoid using IdClass because it would require massive code changes in the project to add all the primary key fields into the entities themselves.

@Embeddable
public class LinkPK {

	@GeneratedValue(generator = "seq_gen", strategy = GenerationType.SEQUENCE)
	@SequenceGenerator(name = "seq_gen", sequenceName = "LK_SQNBR", allocationSize = 1)
	@Column(name = "LK_SQNBR")
	private Integer linkSequenceNbr;

	@Column(name = "LK_FLT_SQNBR", nullable = false, updatable = false)
	private Integer linkFlightSequenceNumber;

Which is used as a primary key for:

@Entity
@Table(name="LINK")
public class Link {
	@EmbeddedId
	private LinkPK id = new LinkPK();

When I persist new “Link” objects I would expect the linkSequenceNbr field to be populated by a value retrieved from the LK_SQNBR sequence. However Hibernate is simply attempting to insert the record with a null value for linkSequenceNbr. I don’t see any select statements to the LK_SQNBR sequence in the logs.

1 Like