Hibernate联合主键 以及根据联合主键中字段查询

这篇博客介绍了如何在Hibernate中处理联合主键,并展示了如何根据联合主键的单个字段进行查询。示例中,`PropertyFileBsline` 类的主键由 `Bslineid` 和 `Bslinepropertyid` 两个字段组成,通过 `Hibernate-mapping` 定义了映射关系。此外,还展示了对应的主键类 `PropertyFileBslinePK` 的构造以及HQL查询语句,如 `from PropertyFileBsline a where a.id.bslineid =?`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hibernate里联合主键配置
  1. <hibernate-mapping package="com.boise">  
  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生成的主键类

  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.   

查询单个主键时用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 =?


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值