Reputation: 2259
I am not able to create Enumerations to be saved as Strings in MySQL DB. I have a MySQL table with a field:
status varchar(50)
Enum
public enum Status {OPEN, CLOSED, CANCELLED, DONE}
Entity
@Column(name="status", nullable=false)
private Status status;
@Enumerated(EnumType.STRING)
public Status getStatus() {return status;}
When I launch my application, I have the following error while fetching data: SQLException: Invalid value for getInt() 'OPEN'
Also I cannot create an Entity, I have a SQLGrammarError. It's trying to save the object with status=OPEN instead of status='OPEN'.
I followed the documentation JavaDoc Persistence Enum And tried this tutorial Jpa and Enums
By adding the @Enumerated(EnumType.STRING) on the attribute status. The fetching error is not there, but I still have the same error to create the entity.
Error Log
[DEBUG] com.vallois.valcrm.web.rest.BusinessResource - REST request to save Business : Business{id=null, name='test', description='okokok', createUpdateInfo=CreateUpdateInfo{createdUsername='user', updatedUsername='null', createdDate=Thu May 21 16:04:54 CEST 2015, updatedDate=null}, status=CLOSED, lock=PUBLIC}
Hibernate: insert into BUSINESS (created_date, created_name, updated_date, u pdated_name, description, lock, name, status, user_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
[WARN] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000
[ERROR] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lock, name, status, user_id) values ('2015-05-21 16:04:54', 'user', null, null, ' at line 1
Upvotes: 3
Views: 6298
Reputation: 24433
Move @Enumerated
to the field
@Column(name="status", nullable=false)
@Enumerated(EnumType.STRING)
private Status status;
EDIT
The exception you are getting is unrelated to enums. lock
is a reserved word in MySQL, you'll have to change it's name to something else.
Upvotes: 9
Reputation: 38152
JPA allows annotations either on fields or on properties, where the place of @Id specifies which of the two is considered.
So in this case I guess you will have to move @Enumerated(EnumType.STRING)
to the field.
Upvotes: 2