Skip to content

Latest commit

 

History

History
96 lines (77 loc) · 3.41 KB

File metadata and controls

96 lines (77 loc) · 3.41 KB

Extended PostgreSQL types for Hibernate 5.0

Features

When this project started I intended to create custom dialect for some ProstgreSQL types that are not included in standard Hibernate library. Then I found that making this as a library with set of custom types is better idea. So please use this project more like an example of adding library with custom types than "final product".

Getting started

Just add a dependency to your project and that is all.

Gradle

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }

    dependencies {
            compile 'com.github.vkuzel:Hibernate-PostgreSQL-Extended-Types:0.5.0'
    }

Maven

    <repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

    <dependency>
	    <groupId>com.github.vkuzel</groupId>
	    <artifactId>Hibernate-PostgreSQL-Extended-Types</artifactId>
	    <version>0.5.0</version>
	</dependency>

Eamples

Create a database table.

    CREATE TABLE test_entity (
      id                     BIGSERIAL,
      flat_array             INT [],
      multidimensional_array INT [] [],
      date                   DATE,
      time                   TIME,
      date_time              TIMESTAMP,
      json                   JSON
    );

Create an entity.

    @Entity
    public class TestEntity {
      @Id
      private long id;
      private List<Integer> flatArray;
      private List<List<Integer>> multidimensionalArray;
      private LocalDate date;
      private LocalTime time;
      private LocalDateTime dateTime;
      private Map<String, Object> json;

      // getters and setters omitted
    }

And then use repository to persist it. Or call a query.

    public interface TestEntityRepository extends JpaRepository<TestEntity, Long> {
      @Query(value = "SELECT flat_array FROM test_entity LIMIT 1", nativeQuery = true)
      List<Integer> array();
    }

Implementation details

PostgreSQL arrays

There is only partial support of PostgreSQL arrays implemented by this library. This means that you can use array (List) types in your entities or queries to select or persist data but Hibernate won't be able to create tables with array columns.

It's because list element types are not available at a runtime. To find out of what type of elements list hold library has to iterate over the list and checks it's elements. Unfortunately this is not a good solution when tables are created. Check out ArrayTypeDescriptor.createPgArray method for more details.

JSON

Mapping of Java type Map<String, Object> to JSON columns is done by Jackson library. Right now project does not support jsonb or mapping to Java objects. Possibly this will be added in future.