Hibernate / JPA - >可以为空的值&对象? [英] Hibernate / JPA -> Nullable values & objects?

查看:112
本文介绍了Hibernate / JPA - >可以为空的值&对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基本问题是:我如何强迫Hibernate分别使float为NULLable和接受NULL为NULL,datetime,blob?我的意思是NULL,而不是(float)0.0。更糟的是,当我尝试存储一个实际为NULL并使用实体管理器的期望的NULLable字段的对象时,我获得属性的错误,即使在db表中,也被Hibernate标记为NULLable。



这是我一直徒劳的尝试:



考虑下表:

  + -------- + ---- ---------- + ------ + ------ + --------- + ---------------- + 
|字段|类型|空| Key |默认|额外|
+ -------- + -------------- + ------ + ----- + -------- - + ---------------- +
| id | int(11)| NO | PRI | NULL | auto_increment |
|名称| varchar(255)| NO | | NULL | |
|数字| float |是| | NULL | |
| url | varchar(3)|是| | NULL | |
| mydate |日期|是| | NULL | |
+ -------- + -------------- + ------ + ----- + -------- - + ---------------- +

这是由这些语句创建:

  DROP SCHEMA如果存在`mydb`; 
如果不存在CREATE SCHEMA`mydb`默认字符集utf8 COLLATE utf8_unicode_ci;
USE`mydb`;

DROP TABLE如果存在`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;

如果我执行以下插入操作:

  INSERT INTO`mydb`.`test`(`name`)VALUES('MyName'); 
SELECT * FROM`mydb`.`test`;

我得到以下结果:

  + ---- + -------- + -------- + ------ + -------- + 
| id |名称|数字| url | mydate |
+ ---- + -------- + -------- + ------ + -------- +
| 1 | MyName | NULL | NULL | NULL |
+ ---- + -------- + -------- + ------ + -------- +
1行(0.00秒)

这正是我想要的。



使用Hibernate 3.5 / JPA2,我尝试像这样做我的ORM:

BO:

  public class BodyStat extends BusinessObject {

@Id
private int id;

私人字符串名称;

@Column(nullable = true)
私人浮动数字;

@Column(nullable = true)
私人网址;

@Column(nullable = true)
私人日期myDate;

... C'Tor / setters / getters / eof

和我的orm.xml中的实体看起来像这样:

 < entity class =test> 
< table name =& quot; test& quot;/>
<属性>
< id name =id>
< generated-value strategy =TABLE/>
< / id>
< basic name =name/>
< basic name =numberoptional =true/>
< basic name =urloptional =true/>
< basic name =myDateoptional =true/>
< / attributes>
< / entity>

现在Hibernate生成的表如下所示:

  + -------- + -------------- + ------ + ----- + --------- + ------------ + 
|字段|类型|空| Key |默认|额外|
+ -------- + -------------- + ------ + ----- + -------- - + ------------ +
| id | int(11)| NO | PRI | NULL | |
|名称| varchar(255)| NO | | NULL | |
|数字| float | NO | | NULL | |
| url | tinyblob |是| | NULL | |
| mydate |日期|是| | NULL | |
+ -------- + -------------- + ------ + ----- + -------- - + ------------ +

这不完全是我想。正如你所看到的那样,


  1. float不为空,
  2. 类型url作为blob而不是varchar,并且
  3. mydate作为date而不是datetime


  4. 解决方案

    再次感谢您的大力支持和建议。其实,我现在已经找到了解决方案。正如帕斯卡所说,我一直在使用注解驱动和xml配置。



    我不会在hibernate上打开一个bug,因为无论如何不要混淆配置类型,因此,我宁愿给出建议来进行最佳实践,而不是混淆配置类型。



    干杯
    ER


    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="&quot;test&quot;"/>
        <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

    1. float is not nullable,
    2. the type "url" turned out to as a blob rather than a varchar and
    3. mydate as "date" rather than "datetime"

    解决方案

    Again thanks for your great support and suggestions. Actually, I have found the solution now. As Pascal mentioned, I had been using both, annotation driven and xml configuration mixed up. As soon as I deleted the xml config, this issue was solved.

    I will not open an bug at hibernate, because it is best practice anyway not to mix up configuration types, hence, I would rather give the advice to carry out best practice than to mix up config types.

    Cheers ER

    这篇关于Hibernate / JPA - &gt;可以为空的值&amp;对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆