Hello,
I have a MySql schema with many tables, with lots of them have fields that are not required.
For example, one such table is
Code:
mysql> desc preference;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| version | int(10) unsigned | NO | | | |
| name | varchar(50) | NO | UNI | | |
| is_ml_text | varchar(1) | NO | | | |
| value | text | NO | | | |
+------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
Code:
mysql> show create table preference;
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| preference | CREATE TABLE `preference` (
`id` int(10) unsigned NOT NULL auto_increment,
`version` int(10) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`is_ml_text` varchar(1) NOT NULL,
`value` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `id` (`id`),
KEY `name_2` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=435 DEFAULT CHARSET=latin1 |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)de]
My business logic is that the name column cannot be empty. A validator makes sure of this.
But the value column can be empty. So does the is_ml_text column.
However, all of these fields are set up with a non null attribute in the table. I had read that it is better to always have such a non null attribute for a field in a MySql table, when I built this schema some years ago.
But now that I'm trying to access this schema with Hibernate, if I don't explicitly set an empty value for the value column and the is_ml_text column, then I get the following exception:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'value' cannot be null
To avoid this exception, I have to explicitly set an empty value when building the object that is persisted, like:
Code:
Preference preference = new Preference();
preference.setName("A_UNIQUE_NAME");
preference.setValue("");
preference.setIsMlText("");
preference = preferenceDao.makePersistent(preference);
But I would prefer NOT to have to explicitly set such empty values so as to avoid coding statements like:
Code:
preference.setValue("");
I then tried to have the default attribute in the mapping file, like:
Code:
<property name="isMlText" type="string">
<column name="is_ml_text" length="1" not-null="false" default="" />
</property>
but the exception remained the same.
I also tried to have the attribute dynamic-insert="true" like:
Code:
<class name="com.thalasoft.learnintouch.core.domain.Preference" table="preference" dynamic-insert="true">
<property name="isMlText" type="string">
<column name="is_ml_text" length="1" not-null="false" default="" />
</property>
but it now gives me the exception:
Caused by: java.sql.SQLException: Field 'is_ml_text' doesn't have a default value
Is there any way, to tell Hibernate, to insert an empty value instead of a NULL value ?
Thanks