Skip to content

A hibernate type that enables persisting list of enums in a single column

License

Notifications You must be signed in to change notification settings

karakays/hibernate-enum-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
)

About

A hibernate type that enables persisting list of enums in a single column

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages