-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to map a Postgres custom enum type with Hibernate
PostPosted: Wed May 25, 2016 10:42 am 
Newbie

Joined: Sat May 21, 2016 4:56 am
Posts: 5
Hi,

Say for eg.
I have an enum user_type defined in database
I have table user and column usertype which is of type user_type. i.e my column type is user defined which is enum.

how to define and use above column in jpa entity via setter and getters?

Also, I have define same enum in java and using like this
@Enumerated(EnumType.STRING)
@Column(name="usertype")
private EnumUserTypes userType;

I am getting below error
org.postgresql.util.PSQLException: ERROR: column "usertype" is of type user_type but expression is of type character varying

how to resolve this?

Thanks.


Top
 Profile  
 
 Post subject: Re: Postgres custom datatype enum with JPA
PostPosted: Wed May 25, 2016 11:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You have two options:

1. You use a custom Hibernate UserType, like this one while you retain the DB column as enum_type.

2. You create a separate table: user_type which has an id column (PK) and an enum_value column which is of type enum_type. The id can also be the String representation if that's what you want.
This way, your mapping mapping will work:

Code:
@Enumerated(EnumType.STRING)
@Column(name="usertype")
private EnumUserTypes userType;


The "usertype" column will have to be a FK to the id in the user_type table. The only drawback is that you have to manually maintain the user_type table, as well as to create the FK because Hibernate cannot do that for you. However, this should be the default schema management strategy anyway, taking that you also use FlywayDb or Liquibase to automate schema migrations.


Top
 Profile  
 
 Post subject: Re: How to map a Postgres custom enum type with Hibernate
PostPosted: Fri May 27, 2016 7:28 am 
Newbie

Joined: Sat May 21, 2016 4:56 am
Posts: 5
Hi,

Thanks, solution -1 works for me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.