欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate视图如何处理空值

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

  举例说明:

  1、表结构及关系

    (1)结构:
    Maintain_form 表
Hibernate视图如何处理空值
            
    
    博客分类: Hibernate配置及应用 HibernateMyeclipse数据结构SQLXML 
    Equipment_dictionary 表
Hibernate视图如何处理空值
            
    
    博客分类: Hibernate配置及应用 HibernateMyeclipse数据结构SQLXML 
    Maintain_equipment表
Hibernate视图如何处理空值
            
    
    博客分类: Hibernate配置及应用 HibernateMyeclipse数据结构SQLXML 

    (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;
	}   
}