Hibernate视图如何处理空值

  现象:项目开发中,根据需求会设计一些比较繁琐的视图,但是使用过程中发现一种奇怪的现象:在SQL语句在数据库中可以查询出正确的结果,但是通过Hibernate查询的结果却为空值;更奇怪的是,HQL查出来的总记录数是正确的,然结果集中却没有对应的数据。
  出现这种现象的原因分析:hibernate 反向工程向导会为视图自动生成一个复合主键, 这个主键是用所有字段加在一起实现的,所以如果其中有一个为空就会报空指针异常。
  解决思路:修改映射的复合型主键,将两张表的主键合起来作为复合型主键,其余字段作为普通值。

  举例说明:

  1、表结构及关系

    (1)结构:
    Maintain_form 表
[img]http://dl.iteye.com/upload/picture/pic/76721/5dc57ee5-38f2-3fdb-b827-2f36b6d508b3.jpg[/img]
    Equipment_dictionary 表
[img]http://dl.iteye.com/upload/picture/pic/76725/e899b54e-628c-34f4-b547-a976438d53e0.jpg[/img]
    Maintain_equipment表
[img]http://dl.iteye.com/upload/picture/pic/76723/f421914d-4e85-34d5-93fc-d64063e4eacc.jpg[/img]

    (2)关系:Maintain_equipment表中的maintain_id作为外键映射Maintain_form 表;Maintain_equipment表中的dictionary_id作为外键映射Equipment_dictionary 表

  2、视图
    CREATE VIEW maintain_all AS select rand(10) AS rand_id, m.maintain_id AS maintain_id, me.dictionary_id AS dictionary_id from maintain_form m left join maintain_equipment me on m.maintain_id = me.maintain_id

    说明:因为:dictionary_id可能为null,所以很多查询结果为空。所以,生成视图后映射文件后做了如下修改:
    (1)dictionary_id 映射的对象提出来作为普通属性。
    (2)符合主键只有:rand_id 和maintain_id;

  3、视图maintain_all的映射文件和javabean

    MaintainAll.hbm.xml
<hibernate-mapping>
<class name="com.equipment.po.MaintainAll" table="maintain_all" catalog="equipment">
<composite-id name="id" class="com.equipment.po.MaintainAllId">
<key-property name="randId" type="java.lang.Double">
<column name="rand_id" precision="22" scale="0" />
</key-property>
<key-many-to-one name="maintainForm" class="com.equipment.po.MaintainForm" lazy="false">
<column name="maintain_id" length="20" />
</key-many-to-one>
</composite-id>
<many-to-one name="dictionary" class="com.equipment.po.EquipmentDictionary" lazy="false" fetch="select">
<column name="dictionary_id" />
</many-to-one>
</class>
</hibernate-mapping>


    MaintainAll.java
package com.equipment.po;


/**
* MaintainAll generated by MyEclipse - Hibernate Tools
*/

public class MaintainAll implements java.io.Serializable {


// Fields

private MaintainAllId id;
private EquipmentDictionary dictionary;


// Constructors

/** default constructor */
public MaintainAll() {
}


/** full constructor */
public MaintainAll(MaintainAllId id,EquipmentDictionary dictionary) {
this.id = id;
this.dictionary = dictionary;
}


// Property accessors

public MaintainAllId getId() {
return this.id;
}

public void setId(MaintainAllId id) {
this.id = id;
}


public EquipmentDictionary getDictionary() {
return dictionary;
}


public void setDictionary(EquipmentDictionary dictionary) {
this.dictionary = dictionary;
}

}


    MaintainAllId.java
package com.equipment.po;


/**
* MaintainAllId generated by MyEclipse - Hibernate Tools
*/

public class MaintainAllId implements java.io.Serializable {


// Fields
private Double randId;
private MaintainForm maintainForm;


// Constructors

/** default constructor */
public MaintainAllId() {
}

/**
* @param maintainForm
* @param dictionary
*/
public MaintainAllId(MaintainForm maintainForm) {
this.maintainForm = maintainForm;

}



// Property accessors



public MaintainForm getMaintainForm() {
return maintainForm;
}

public void setMaintainForm(MaintainForm maintainForm) {
this.maintainForm = maintainForm;
}

public Double getRandId() {
return randId;
}

public void setRandId(Double randId) {
this.randId = randId;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值