Description: Hibernate Types is a set of extra types not supported by default in Hibernate Core. One of these types is java.time.YearMonth. This is a Spring Boot application that uses Hibernate Type to store this YearMonth in a MySQL database as integer or date.

Key points:

  • for Maven, add Hibernate Types as a dependency in pom.xml
  • in entity use @TypeDef to map typeClass to defaultForType

typeClass = YearMonthIntegerType.class, // or, YearMonthDateType
defaultForType = YearMonth.class

public class Book implements Serializable {

private static final long serialVersionUID = 1L;

@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;
private String isbn;
private YearMonth releaseDate;

public interface BookRepository extends JpaRepository<Book, Long> {
Book findByTitle(String title);

public class BookstoreService {

private final BookRepository bookRepository;

public void newBook() {
Book book = new Book();
book.setTitle("Young Boy");

public void displayBook() {
Book book = bookRepository.findByTitle("Young Boy");

public class MainApplication {

private final BookstoreService bookstoreService;

public static void main(String[] args) {, args);

public ApplicationRunner init() {
return args -> {



Console Output —

Hibernate: select as id1_0_, book0_.isbn as isbn2_0_, book0_.release_date as release_3_0_, book0_.title as title4_0_ from book book0_ where book0_.title=?
Book(id=1, title=Young Boy, isbn=001, releaseDate=2021-06)




Java Developer and enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to use data version control (dvc) in a machine learning project

Microservices architecture -1

What’s Next? Watching Netflix with Help from Accessibility Services

Role of start() and run() in Multithreading in Java

Amazon Product Advertising API

Playing with Data Frames — Part 2 (Post #5/365)

PROJECT (Sugar Cosmetics Website)

How Visual Regression Testing Can Help You Deliver Better Apps — Applitools Blog

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Java Developer and enthusiast

More from Medium

JPA Key Notes

Protect Jar/War project with Java VME mode

Creating a Java project using Maven archetype

Creating database seeder in spring boot application