I have created a UserPreferences
bean. The primary key being id
column. Here is the definition of the bean:
@Entity
public class UserPreferences {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(unique = true)
@ColumnDefault("")
private String id;
private String email;
// Getters and Setters
}
Now I use Kotlin
to insert data in this bean. Here is the code to do so:
val newUserPreferences = UserPreferences().apply { email = newUserRequest.email }
mUserPreferenceService.save(newUserPreferences)
Lets assume that newUserRequest.email
is not null
. The auto-dll
setting in my application.yml
is update
. When this code is executed, I see that Hibernate
generates following query for creating the table:
create table user_preferences (
id varchar(255) default not null
email varchar(255),
primary key (id)
) engine=MyISAM
At this point, the application fails to load with the following error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'not null,
Looking at the exception, it is very clear that the code is failing because instead of using an empty String
as default value, hibernate is simply putting a blank space in the generated query. So here are my questions:
- Should this be treated as a bug in Hibernate?
- How to handle this situation where I would like to have an empty String
""
as column default value?