2 heiyee heiyee 于 2013.10.07 16:45 提问

关于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个回答

yy8093
yy8093   2015.01.26 14:28

Topic.user_id 的setter有问题

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!