I'm using hibernate with @Embeddable for composite keys. I have 2 classes for a test:
@Entity
@Table(name = "da_form_view")
@Data
public class FormView {
@EmbeddedId
FormViewPk formViewPk;
}
@Embeddable
@Data
public class FormViewPk implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = " int(10) unsigned NOT NULL AUTO_INCREMENT")
private Long id;
@Column(name = "curtime", columnDefinition = " int(10) unsigned NOT NULL DEFAULT '0'")
private Integer curtime;
}
@Repository
@Transactional
public interface FormViewRepository extends JpaRepository<FormView, FormViewPk> {
}
When I do a FormViewRepository, save or saveAndFlush, I don't get the FormViewPk.id updated with the value that has been inserted in the DB, it returns a null for that value. In this case the data base is H2 with autocommit, and I need both keys because that table is using partitions (I can't use only the GeneratedValue as unique key).
Any idea?
CodePudding user response:
According to the documentation this is not possible.
When using composite identifiers, the underlying identifier properties must be manually assigned by the user.
Automatically generated properties are not supported to be used to generate the value of an underlying property that makes the composite identifier.
Therefore, you cannot use any of the automatic property generator described by the generated properties section like @Generated, @CreationTimestamp or @ValueGenerationType or database-generated values.