jsjgelei
2013-06-21 10:25
浏览 293
已采纳

hibernate问题

问题:我通过hibernate原生sql子查询获取返回的实体list
异常:
Could not find [color=red]setter [/color]for AD_ID on class com.gtd.rocket.entity.AddressEntity
Could not find setter for AD_ID on class com.gtd.rocket.entity.AddressEntity; nested exception is org.hibernate.PropertyNotFoundException: Could not find setter for AD_ID on class com.gtd.rocket.entity.AddressEntity
分析:实体已经有setter方法
@Id
@GeneratedValue
@Column(name = "ad_id", unique = true, nullable = true)
public int getAd_id()
{
return ad_id;
}
public void setAd_id(int ad_id)
{
this.ad_id = ad_id;
}
sql查询:
String sql = "select * from a_address where ad_id in(:ids) order by ad_id";
Map> params = new HashMap>();
params.put("ids", idlist);

public List executeListBySql(final boolean transformer, final String queryString, final Map values)
{
return getHibernateTemplate().executeFind(new HibernateCallback()
{

        public Object doInHibernate(Session session) throws HibernateException, SQLException
        {
            Query query = session.createSQLQuery(queryString);
            if (transformer)
            {
                query.setResultTransformer(Transformers.aliasToBean(entityClass));
            }
            if (values != null && values.size() > 0)
            {
                query.setProperties(values);
            }
            List list = query.list();
            return list;
        }
    });
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • jinnianshilongnian 2013-06-21 10:53
    已采纳

    官网上有解释

    Could not find setter for AD_ID 即因为如使用oracle默认不区分大小写,而且它没有把你的EntityClass识别为Entity所以无法转换为property,所以默认就调用setAD_ID;
    1、调用如下 告诉hibernate我是个Entity
    sess.createSQLQuery("SELECT {cat.*} FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
    .addEntity("cat", Cat.class)

    3、 select A_ID as "a_id" 如果是mysql 使用a_id 告诉hibernate 真正的属性名 区分大小写的

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题