heiyee 2013-10-07 08:45 采纳率: 0%
浏览 2177

关于HQL和SQL查询的问题

这是配置文件user.hbm
`

  <class name="com.entity.User" table="user">
                        <id name="email" column="email" type="string" length="100">
                            <generator class="assigned"></generator>
                        </id>
                        <property name="username" column="username" type="string"></property>
                        <property name="age" column="age" type="integer"></property>
                        <property name="sex" column="sex" type="integer"></property>

                        <set name="topic" cascade="all">
                            <key column="user_id" foreign-key="email"></key>
                            <one-to-many class="com.jiandan.entity.Topic" />
                        </set>                      
 </class>`

这是topic类的配置文件

 <class name="com.entity.Topic" table="topic">
                    <id column="id" name="id" type="string" length="100">
                        <generator class="uuid"></generator>
                    </id>
                    <property name="content" column="content" type="string"></property>
                    <property name="post_date" column="post_date" type="java.util.Date"></property>
                    <many-to-one class="com.jiandan.entity.User" name="user_id" column="user_id" ></many-to-one>
                  <set name="response" cascade="all">
                        <key column="topic_id"></key>
                        <one-to-many class="com.jiandan.entity.Response"/>
                    </set>
   </class> 

当我使用这种查询方式时就会出错:

Query query = session.createQuery("from Topic where user_id='Clement@gmail.com'");
List topic = query.list();
for(Topic t : topic){
System.out.println(t.getContent());
}
错误信息:

2013-10-7 16:34:24 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000123: IllegalArgumentException in class: com.jiandan.entity.Topic, setter method of property: user_id
2013-10-7 16:34:24 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000091: Expected type: java.lang.String, actual value: com.jiandan.entity.User_$$_javassist_0
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.jiandan.entity.Topic.user_id

但是使用sql的方式却可以查出结果:

SQLQuery sql = session.createSQLQuery("select * from Topic where user_id = 'Clement@gmail.com'");
        List<Object[]> topic = sql.list();
        for(Object[] t : topic){
            int len = t.length;
            for(int i=0; i<len;i++){
                System.out.println(t[i]);
            }
        }

谁能帮我解决这个问题,非常感谢!

  • 写回答

1条回答

  • 我说假如 2015-01-26 06:28
    关注

    Topic.user_id 的setter有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?