[置顶]       hibernate复合主键查询主键不能为空的解决方案

实体类:

[java] view plain copy
  1. public class SubStationLog  implements java.io.Serializable {  
  2.       
  3.     private  Long substationid;  
  4.     private String markDate;  
  5.         private String mark;  
  6.   
  7. //省去构造 set/get方法  
  8.   
  9. }  


原配置:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4.   
  5. <hibernate-mapping>  
  6.     <class name="com.adam.dev.entity.SubStationLog" table="POB_SUBSTATION_LOG" schema="JMJL">  
  7.         <composite-id>  
  8.             <key-property name="markDate" type="string">  
  9.                 <column name="MARK_DATE" length="20" />  
  10.             </key-property>  
  11.            <key-property name="substationid" type="long">  
  12.                 <column name="SUBSTATIONID" precision="10" scale="0" />  
  13.             </key-property>  
  14.         </composite-id>  
  15.   
  16.         <property name="mark" type="string">  
  17.             <column name="MARK" length="10" />  
  18.         </property>  
  19.     </class>  
  20. </hibernate-mapping>  


修改后配置:

[html] view plain copy
  1. <hibernate-mapping>  
  2.     <class name="com.adam.dev.entity.SubStationLog" table="POB_SUBSTATION_LOG" schema="JMJL">  
  3.     <composite-id>  
  4.         <key-property name="substationid" type="long">  
  5.                 <column name="SUBSTATIONID" precision="10" scale="0" />  
  6.             </key-property>  
  7.         </composite-id>  
  8.   
  9.     <property name="markDate" type="string" >  
  10.                 <column name="MARK_DATE" length="20" />  
  11.          </property>  
  12.   
  13.         <property name="mark" type="string">  
  14.             <column name="MARK" length="10" />  
  15.         </property>  
  16.     </class>  
  17. </hibernate-mapping>  
显然是将联合主键中的markDate移到了<composite-id>标签对之外,这样便可以避免因为联合主键查询一查询条件为空,便不显示符合条件记录的情况。

你可能感兴趣的:(Hibernate,hibernate联合主键查询)