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

实体类:

public class SubStationLog  implements java.io.Serializable {
	
	private  Long substationid;
	private String markDate;
        private String mark;

//省去构造 set/get方法

}


原配置:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.adam.dev.entity.SubStationLog" table="POB_SUBSTATION_LOG" schema="JMJL">
		<composite-id>
            <key-property name="markDate" type="string">
                <column name="MARK_DATE" length="20" />
            </key-property>
		   <key-property name="substationid" type="long">
                <column name="SUBSTATIONID" precision="10" scale="0" />
            </key-property>
        </composite-id>

        <property name="mark" type="string">
            <column name="MARK" length="10" />
        </property>
    </class>
</hibernate-mapping>


修改后配置:

<hibernate-mapping>
    <class name="com.adam.dev.entity.SubStationLog" table="POB_SUBSTATION_LOG" schema="JMJL">
	<composite-id>
	    <key-property name="substationid" type="long">
                <column name="SUBSTATIONID" precision="10" scale="0" />
            </key-property>
        </composite-id>

	<property name="markDate" type="string" >
                <column name="MARK_DATE" length="20" />
         </property>

        <property name="mark" type="string">
            <column name="MARK" length="10" />
        </property>
    </class>
</hibernate-mapping>
显然是将联合主键中的markDate移到了<composite-id>标签对之外,这样便可以避免因为联合主键查询一查询条件为空,便不显示符合条件记录的情况。



你可能感兴趣的:(Hibernate)