MISSJUDY 2016-11-24 14:09 采纳率: 100%
浏览 1474
已采纳

Hibernate单向one-to-one关联查询 - provided id of the wrong type for class

---------------------Table设定信息-----------------------
Table --- Car

carId -- number -- pk 序列号
brandId -- varchar2 - fk 汽车品牌

Table --- CarBrand

brandId -- varchar2 -- pk 品牌ID
brandName -- varchar2 品牌名称

----------------------Hbm设定信息---------------------------
Car.hbm

 <id name="carId">
    <generator class="sequence">s_car_id</generator>
 </id>

 <one-to-one name="brand" class="CarBrand.class" />

CarBrand.hbm

 <id name="brandId">
   <generator class="assigned"/>
 </id>

 <property name="brandName" type="java.lang.String" column="brandName"/>

---------------------查询所有品牌为A的车辆---------------------
Criteria c = session.createCriteria();
c.createAlias(Car.class, "car");
c.createAlias("car.brand","brand");
c.add(Restriction.eq("brand.brandId","A"));'
c.list();

执行过程中产生如下报错信息
org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.lw.domiane.Car Expected: class java.lang.String , got class java.lang.Long

at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:109)

at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)

at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)

at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)

at com.lw.service.imp.BaseService.findById(BaseService.java:55)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)

at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)

at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)

at $Proxy12.findById(Unknown Source)

at com.lw.web.action.CarAction.download()(CarAction.java:51)

以上我出现的状况,有谁能帮忙解决一下,谢谢!

  • 写回答

2条回答

  • MISSJUDY 2016-11-25 13:17
    关注

    看来没人能回答我的问题,研究了一下,终于解决了,这里分享给大家。
    首先大家可以从我的表的设定看出来,brandId是一个外键,关联到carBrand表里面的brandId,我现在希望使用one-to-one的映射模式使得Car.class的类中能够产生一个Brand的对象;
    有两种映射模式,主键关联模式以及唯一外键模式
    主键关联就是我们正常情况下使用的

     <one-to-one name="brand" class="CarBrand.class" />
    
    

    这种情况下产生的sql的形式如下:
    select * from car c, carBrand cb where c.carId = cb.brandId ;

    这就是我使用上面的方式报错的原因;

    唯一外键模式,hbm格式如下

    <many-to-one name="brand" class="CarBrand.class" unique="true" column="brandId"/>
    
    

    此模式下产生sql如下
    select * from car c, carBrand cb where c.barndId = cb.brandId

    将Car.hbm文件中配置修改掉就可以解决这个问题了,希望对大家有帮助。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题