Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Enum property - annotation on getter fails
PostPosted: Fri Jun 02, 2017 6:02 pm 
Newbie

Joined: Fri May 05, 2017 12:09 pm
Posts: 7
I have a table with a `status: varchar` column. Java entity class has property:

Code:
public enum Status { ENABLED, DISABLED }
private Status status;
@Enumerated(EnumType.STRING)
public Status getStatus() {
   return status;
}


That fails. But it works if I move the `@Enumerated` annotation to the private field, instead of the getter. Is that expected behavior?

The error when the annotation is on the getter: org.postgresql.util.PSQLException: Bad value for type int : ENABLED

I stepped through code and can see it's treating the enum property as if it didn't have the `@Enumerated` and it's defaulting to `EnumType.ORDINAL`.

I thought it was acceptable, or even preferred, to put the annotations on the getters, not the fields.

Rob


Top
 Profile  
 
 Post subject: Re: Enum property - annotation on getter fails
PostPosted: Sat Jun 03, 2017 1:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1385
As explained in the User Guide, the placement of the @Id gives the access strategy.

In your case, most likely the @Id is on a field, so you have two options:

1. Either you move all annotations to getters, including thee @Id.
2. You leave the @Id on the field, but use the @Access annotation:

Code:
@Access( AccessType.PROPERTY )
@Enumerated(EnumType.STRING)
public Status getStatus() {
   return status;
}

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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.