How to emulate @CreatedBy and @LastModifiedBy from Spring Data using the @GeneratorType Hibernate annotation

Introduction

Hibernate comes with many additions to the standard JPA specification. One such example is the @GeneratorType annotation which allows you to customize the way a given entity property value is automatically generated.

If you’re using Spring Data, you can simply use the @CreatedBy and @LastModifiedBy annotations and the annotated entity properties are going to be populated with the currently logged user.

If you’re not using Spring Data, then you can easily emulate the same behavior using the Hibernate-specific @GeneratorType annotation and the ValueGenerator callback mechanism.

Continue reading “How to emulate @CreatedBy and @LastModifiedBy from Spring Data using the @GeneratorType Hibernate annotation”

Advertisements

How to map a composite identifier using an automatically @GeneratedValue with JPA and Hibernate

Introduction

One of my readers asked me to answer the following StackOverflow question.

While I already covered the best way to map composite identifiers with JPA and Hibernate, this use case is different because one column is automatically generated.

Continue reading “How to map a composite identifier using an automatically @GeneratedValue with JPA and Hibernate”