Skip to content

Commit 9db5658

Browse files
committed
Mapping PostgreSQL enum via Hibernate EnumType
1 parent 0693392 commit 9db5658

File tree

11 files changed

+252
-0
lines changed

11 files changed

+252
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**[How To Map Java `enum` To Database Via `AttributeConverter`](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootEnumAttributeConverter)**
2+
3+
**Description:** This application maps a Java `enum` via `AttributeConverter`. In other words, it maps the `enum` values `HORROR`, `ANTHOLOGY` and `HISTORY` to the integers `1`, `2` and `3` and viceversa. This allows us to set the column type as `SMALLINT` which is less space-consuming than `VARCHAR(9)` needed in this case.
4+
5+
**Key points:**\
6+
- define a custom `AttributeConverter`\
7+
- annotate with `@Converter` the corresponding entity field
8+
9+
<a href="https://leanpub.com/java-persistence-performance-illustrated-guide"><p align="center"><img src="https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/Java%20Persistence%20Performance%20Illustrated%20Guide.jpg" height="410" width="350"/></p></a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>HibernateSpringBootEnumPostgreSQLCustomType</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>HibernateSpringBootEnumPostgreSQLCustomType</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-jdbc</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.postgresql</groupId>
42+
<artifactId>postgresql</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.bookstore;
2+
3+
import com.bookstore.service.BookstoreService;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class MainApplication {
11+
12+
private final BookstoreService bookstoreService;
13+
14+
public MainApplication(BookstoreService bookstoreService) {
15+
this.bookstoreService = bookstoreService;
16+
}
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(MainApplication.class, args);
20+
}
21+
22+
@Bean
23+
public ApplicationRunner init() {
24+
return args -> {
25+
bookstoreService.persistNewAuthor();
26+
bookstoreService.fetchAuthor();
27+
};
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.bookstore.entity;
2+
3+
import com.bookstore.enums.GenreType;
4+
import java.io.Serializable;
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.EnumType;
8+
import javax.persistence.Enumerated;
9+
import javax.persistence.GeneratedValue;
10+
import javax.persistence.GenerationType;
11+
import javax.persistence.Id;
12+
import org.hibernate.annotations.Type;
13+
14+
@Entity
15+
public class Author implements Serializable {
16+
17+
private static final long serialVersionUID = 1L;
18+
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.IDENTITY)
21+
private Long id;
22+
23+
private int age;
24+
private String name;
25+
26+
@Enumerated(EnumType.STRING)
27+
@Type(type = "genre_enum_type")
28+
@Column(columnDefinition = "genre_info")
29+
private GenreType genre;
30+
31+
public Long getId() {
32+
return id;
33+
}
34+
35+
public void setId(Long id) {
36+
this.id = id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public GenreType getGenre() {
48+
return genre;
49+
}
50+
51+
public void setGenre(GenreType genre) {
52+
this.genre = genre;
53+
}
54+
55+
public int getAge() {
56+
return age;
57+
}
58+
59+
public void setAge(int age) {
60+
this.age = age;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Author{" + "id=" + id + ", age=" + age
66+
+ ", name=" + name + ", genre=" + genre + '}';
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.bookstore.enums;
2+
3+
public enum GenreType {
4+
5+
HORROR, ANTHOLOGY, HISTORY
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bookstore.repository;
2+
3+
import com.bookstore.entity.Author;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface AuthorRepository extends JpaRepository<Author, Long> {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.bookstore.service;
2+
3+
import com.bookstore.entity.Author;
4+
import com.bookstore.enums.GenreType;
5+
import com.bookstore.repository.AuthorRepository;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class BookstoreService {
10+
11+
private final AuthorRepository authorRepository;
12+
13+
public BookstoreService(AuthorRepository authorRepository) {
14+
this.authorRepository = authorRepository;
15+
}
16+
17+
public void persistNewAuthor() {
18+
Author author = new Author();
19+
20+
author.setName("Maryus Yarn");
21+
author.setAge(34);
22+
author.setGenre(GenreType.HORROR);
23+
24+
authorRepository.save(author);
25+
}
26+
27+
public void fetchAuthor() {
28+
Author author = authorRepository.findById(1L).orElseThrow();
29+
System.out.println(author);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bookstore.type;
2+
3+
import java.sql.PreparedStatement;
4+
import java.sql.SQLException;
5+
import java.sql.Types;
6+
import org.hibernate.HibernateException;
7+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
8+
import org.hibernate.type.EnumType;
9+
10+
public class PostgreSQLEnumType extends EnumType {
11+
12+
@Override
13+
public void nullSafeSet(PreparedStatement ps, Object obj, int index,
14+
SharedSessionContractImplementor session) throws HibernateException, SQLException {
15+
if (obj == null) {
16+
ps.setNull(index, Types.OTHER);
17+
} else {
18+
ps.setObject(index, obj.toString(), Types.OTHER);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@org.hibernate.annotations.TypeDef(name = "genre_enum_type", typeClass = PostgreSQLEnumType.class)
2+
3+
package com.bookstore.type;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
2+
spring.datasource.username=postgres
3+
spring.datasource.password=postgres
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
9+
10+
spring.datasource.initialization-mode=always
11+
spring.datasource.platform=postgresql
12+
13+
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
14+
15+
spring.jpa.open-in-view=false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TYPE IF EXISTS genre_info CASCADE;
2+
CREATE TYPE genre_info AS ENUM ('HORROR', 'ANTHOLOGY', 'HISTORY')

0 commit comments

Comments
 (0)