com.karakays.hibernate:hibernate-enum-array

Persist enums as native array type in a single column

License

License

Categories

Categories

Hibernate Data ORM
GroupId

GroupId

com.karakays.hibernate
ArtifactId

ArtifactId

hibernate-enum-array
Last Version

Last Version

0.1
Release Date

Release Date

Type

Type

jar
Description

Description

com.karakays.hibernate:hibernate-enum-array
Persist enums as native array type in a single column
Project URL

Project URL

https://github.com/karakays/hibernate-enum-array/
Source Code Management

Source Code Management

https://github.com/karakays/hibernate-enum-array/

Download hibernate-enum-array

How to add to project

<!-- https://jarcasting.com/artifacts/com.karakays.hibernate/hibernate-enum-array/ -->
<dependency>
    <groupId>com.karakays.hibernate</groupId>
    <artifactId>hibernate-enum-array</artifactId>
    <version>0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.karakays.hibernate/hibernate-enum-array/
implementation 'com.karakays.hibernate:hibernate-enum-array:0.1'
// https://jarcasting.com/artifacts/com.karakays.hibernate/hibernate-enum-array/
implementation ("com.karakays.hibernate:hibernate-enum-array:0.1")
'com.karakays.hibernate:hibernate-enum-array:jar:0.1'
<dependency org="com.karakays.hibernate" name="hibernate-enum-array" rev="0.1">
  <artifact name="hibernate-enum-array" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.karakays.hibernate', module='hibernate-enum-array', version='0.1')
)
libraryDependencies += "com.karakays.hibernate" % "hibernate-enum-array" % "0.1"
[com.karakays.hibernate/hibernate-enum-array "0.1"]

Dependencies

compile (4)

Group / Artifact Type Version
org.hibernate : hibernate-core jar 5.2.12.Final
org.hibernate.javax.persistence : hibernate-jpa-2.1-api jar 1.0.0.Final
com.querydsl : querydsl-apt jar 4.1.4
org.projectlombok : lombok jar 1.16.16

provided (1)

Group / Artifact Type Version
org.postgresql : postgresql jar 9.4.1212

test (2)

Group / Artifact Type Version
junit : junit-dep jar 4.10
org.hamcrest : hamcrest-library jar 1.3

Project Modules

There are no modules declared in this project.

hibernate-enum-array

Build Status Maven Central License

Read/write an array of Java enums into a database table field without any join tables.

Currently supported databases:

  • Postgresql

Installation

<dependency>
    <groupId>com.karakays.hibernate</groupId>
    <artifactId>hibernate-enum-array</artifactId>
    <version>0.1</version>
</dependency>

Example

@Entity
public class User {
    @Column(name = "badges", columnDefinition="text[]")
    @Type(type = "com.karakays.hibernate.array.EnumArrayType",
            parameters = { @Parameter(name="enumClass", value="com.karakays.hibernate.array.domain.User$Badge") })
    private List<Badge> badges;
}

where Badge is a custom Enum

public enum Badge {
    GURU, MASTER, CHALLENGER, ORACLE;
}

and its type is passed along using org.hibernate.annotations.Parameter annotation.

Now you can persist a list of enums in a table field with no further relation tables.

Please see the test class to see a full example.

DDL generation

Considering this class:

@Table(name = "users")
@Entity
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    @Column(name = "badges", columnDefinition="text[]")
    @Type(type = "com.karakays.hibernate.array.EnumArrayType",
            parameters = { @Parameter(name="enumClass", value="com.karakays.hibernate.array.domain.User$Badge") })
    private List<Badge> badges;
}

Following DDL is generated:

create table users (
    id int8 not null,
    name varchar(255),
    badges text[],
    primary key (id)
)

Versions

Version
0.1