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条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3