Hi
My basic question is: How can I force Hibernate to make float NULLable and accept NULL for float, datetime, blob, respectively? I really mean NULL, not (float) 0.0.
Even worse, when I try to store an object with the desired NULLable fields actually being NULL and using entity manager, I get errors for attributes, which are marked as NULLable by Hibernate even in the db table.
Here is what I have been trying in vain:
Consider this table:
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| number | float | YES | | NULL | |
| url | varchar(3) | YES | | NULL | |
| mydate | date | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
which was created by these statements:
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
USE `mydb` ;
DROP TABLE IF EXISTS `mydb`.`test` ;
CREATE TABLE IF NOT EXISTS `mydb`.`test` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NOT NULL ,
`number` FLOAT NULL,
`url` VARCHAR(3) NULL ,
`mydate` DATE NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;
If I carry out following insert:
INSERT INTO `mydb`.`test` (`name`) VALUES ('MyName');
SELECT * FROM `mydb`.`test`;
I get following result:
+----+--------+--------+------+--------+
| id | name | number | url | mydate |
+----+--------+--------+------+--------+
| 1 | MyName | NULL | NULL | NULL |
+----+--------+--------+------+--------+
1 row in set (0.00 sec)
which is exactly what I want.
With Hibernate 3.5 / JPA2, I try do do my ORM like this:
BO:
public class BodyStat extends BusinessObject {
@Id
private int id;
private String name;
@Column(nullable = true)
private float number;
@Column(nullable = true)
private URL url;
@Column(nullable = true)
private Date myDate;
... C'Tor / setters / getters / eof
and the entity in my orm.xml looks like this:
<entity class="test">
<table name=""test""/>
<attributes>
<id name="id">
<generated-value strategy="TABLE"/>
</id>
<basic name="name"/>
<basic name="number" optional="true"/>
<basic name="url" optional="true"/>
<basic name="myDate" optional="true"/>
</attributes>
</entity>
Now the table generated by Hibernate looks like this:
+--------+--------------+------+-----+---------+------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+------------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(255) | NO | | NULL | |
| number | float | NO | | NULL | |
| url | tinyblob | YES | | NULL | |
| mydate | date | YES | | NULL | |
+--------+--------------+------+-----+---------+------------+
which is not quite what I want. As you can see
- float is not nullable,
- the type "url" turned out to as a blob rather than a varchar and
- mydate as "date" rather than "datetime"