hibernate里联合主键composite-id映射,查询单个主键的问题(转)

hibernate里联合主键composite-id映射,查询单个主键的问题(转)

- 今天项目中遇到这个问题,搞了大半天,我郁闷...
hibernate里联合主键配置(多个字段一起作为主键)
  1. <hibernate-mapping package="com.hp.orm.bo">  
  2.     <class name="PropertyFileBsline" table="property_file_bsline">  
  3.        <composite-id name="Id" class="PropertyFileBslinePK">  
  4.           <key-property name="Bslineid" column="bslineid" type="string"/>  
  5.           <key-property name="Bslinepropertyid" column="bslinepropertyid"  
  6.     type="string"/>  
  7.        </composite-id>  
  8.        <property   
  9.           name="Bslinepropertyname"  
  10.           column="bslinepropertyname"  
  11.           type="string"  
  12.           not-null="true"  
  13.           length="20"/>  
  14.        <property  
  15.           name="Bslinepropertytype"  
  16.           column="bslinepropertytype"  
  17.           type="string"  
  18.           not-null="true"  
  19.           length="20"/>  
  20.        <property  
  21.           name="Flag"   
  22.           column="flag"  
  23.           type="string"  
  24.           not-null="true"  
  25.          length="10"/>  
  26.     </class>     
  27. </hibernate-mapping>  
<hibernate-mapping package="com.hp.orm.bo"> <class name="PropertyFileBsline" table="property_file_bsline"> <composite-id name="Id" class="PropertyFileBslinePK"> <key-property name="Bslineid" column="bslineid" type="string"/> <key-property name="Bslinepropertyid" column="bslinepropertyid" type="string"/> </composite-id> <property name="Bslinepropertyname" column="bslinepropertyname" type="string" not-null="true" length="20"/> <property name="Bslinepropertytype" column="bslinepropertytype" type="string" not-null="true" length="20"/> <property name="Flag" column="flag" type="string" not-null="true" length="10"/> </class> </hibernate-mapping>
Hibernate生成的主键类
  1. public class PropertyFileBslinePK extends BasePropertyFileBslinePK {   
  2.     private static final long serialVersionUID = 1L;   
  3.   
  4. /*[CONSTRUCTOR MARKER BEGIN]*/  
  5.     public PropertyFileBslinePK () {}   
  6.        
  7.     public PropertyFileBslinePK (   
  8.          java.lang.String bslineid,   
  9.          java.lang.String bslinepropertyid) {   
  10.   
  11.         super (   
  12.              bslineid,   
  13.              bslinepropertyid);   
  14.      }   
  15. /*[CONSTRUCTOR MARKER END]*/  
  16.   
  17. }  
public class PropertyFileBslinePK extends BasePropertyFileBslinePK { private static final long serialVersionUID = 1L; /*[CONSTRUCTOR MARKER BEGIN]*/ public PropertyFileBslinePK () {} public PropertyFileBslinePK ( java.lang.String bslineid, java.lang.String bslinepropertyid) { super ( bslineid, bslinepropertyid); } /*[CONSTRUCTOR MARKER END]*/ }
查询单个主键时用HQL语句
  1. from PropertyFileBsline a where a.id.bslineid =?  
from PropertyFileBsline a where a.id.bslineid =?
结果报错:
NoClassDefFoundError: org/hibernate/exception/DataException
上网查了一下,说是hql语句的问题.
测试了大半天结果调试正常了,原来是a.id.bslineid =?中的bslineid 要写映射xml中的name,不能写PK类中的属性.
正确的HQL:
  1. from PropertyFileBsline a where a.id.Bslineid =?  
from PropertyFileBsline a where a.id.Bslineid =?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值