betorcs
betorcs

Reputation: 2831

Is it possible set `null` value for non mapped columns using JPA/Hibernate?

I have an existing table with a lot of fields, some of them without default value, like bellow.

In my entity class i just want to declare some fields.

When i try to insert a new row, i'm getting the error Field 'hours_work' doesn't have a default value.

+------------------------+---------------+------+-----+---------+----------------+
| Field                  | Type          | Null | Key | Default | Extra          |
+------------------------+---------------+------+-----+---------+----------------+  
|  ...                                                                  
| hours_work             | text          | NO   |     | NULL    |                |
| locations              | text          | NO   |     | NULL    |                |
| ...                                                                                            

Then, i set sql_mode to ''

mysql> set @@sql_mode='';
Query OK, 0 rows affected (0.00 sec)

So I'm able to insert row by command line, but not using my application

mysql> insert into MyTable (column1) values ('test value');
Query OK, 1 row affected, 84 warnings (0.00 sec)

I tried set sql_mode in URL connection like i saw in some posts, but didn't work

application.yml

main:
  datasource:
    url: jdbc:mysql://localhost/my_database?sessionVariables=sql_mode=''

I appreciate any suggestion!

Upvotes: 0

Views: 278

Answers (1)

betorcs
betorcs

Reputation: 2831

I was able to fix it addingjdbcCompliantTruncation=false to MySQL URL connection, according to Sébastien explanation below.

Sébastien,

The driver needs the STRICT_TRANS_TABLES mode enabled to enforce JDBC compliance on truncation checks.

If you can't use STRICT_TRANS_TABLES as part of your sql_mode, then you'll have to disable truncation checks by adding "jdbcCompliantTruncation=false" as a URL configuration parameter.

According to Sébastien, at https://bugs.mysql.com/bug.php?id=23371

So now URL connection looks likes

main:
  datasource:
    url: jdbc:mysql://localhost/edirectory_domain?jdbcCompliantTruncation=false&sessionVariables=sql_mode=''

Upvotes: 1

Related Questions