lfyy1 2009-01-20 20:56
浏览 1284
已采纳

setResultTransformer(Transformers.aliasToBean(..))

以前一直用jdbc做开发,取出的数据放在为每个表建立的数据类中,想放表的哪几个字段就放哪几个
现在想学学hibernate,用oracle的原生sql想取出一个表中的两列:
String selectTopicSql = "select topic,sectorname from article order by topic desc";
query = session.createSQLQuery(selectTopicSql1).setResultTransformer(Transformers.aliasToBean(Article.class));
Article.java就是hibernate对表ARTICLE的映射类,其配置文件如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

name="Article"
table="ARTICLE"
>
false
name="topic"
column="TOPIC"
type="string">


name="Sectorname"
column="SECTORNAME"
type="string"
not-null="true"
length="40"
/>
name="Contents"
column="CONTENTS"
type="string"
/>
name="Keyword"
column="KEYWORD"
type="string"
length="40"
/>
name="Source"
column="SOURCE"
type="string"
length="100"
/>
name="Pageview"
column="PAGEVIEW"
type="integer"
length="10"
/>
name="Score"
column="SCORE"
type="big_decimal"
length="2"
/>
name="Createtime"
column="CREATETIME"
type="date"
length="7"
update="false"
/>
name="Updatetime"
column="UPDATETIME"
type="date"
length="7"
update="false"
/>



该配置文件在执行全字段检索没有问题,现在只取topic,sectorname就报错:
Exception in thread "main" org.hibernate.PropertyNotFoundException: Could not find setter for TOPIC on class test.Article
at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:68)
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:87)
at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:92)
at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:353)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1722)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)
at com.struts_learn.dao.ArticleDAO.selectTopic(ArticleDAO.java:58)
at com.struts_learn.dao.ArticleDAO.main(ArticleDAO.java:206)
看了源码没看明白,如果另外再写一个jdbc那种数据类,何必用hibernate的映射类,如果不这样是不是就不能只取两个字段?我那个表的其他字段有很大的,不想全取出来,一直听说hibernate是持久层牛X框架,今日一见实在是失望,搞这么个东西在jdbc不用10分钟,竟然在hibernate里搞了半天也没能用映射类Article.java实现!
希望有对hibernate老手指点下!
package test;

import test.base.BaseArticle;
import java.math.*;

public class Article extends BaseArticle {
private static final long serialVersionUID = 1L;

/*[CONSTRUCTOR MARKER BEGIN]*/
public Article () {
super();
}

/**
 * Constructor for primary key
 */
public Article (java.lang.String topic) {
    super(topic);
}

/**
 * Constructor for required fields
 */
public Article (
    java.lang.String topic,
    java.lang.String sectorname,
    java.lang.String contents,
    java.lang.String keyword,
    java.lang.String source,
    java.lang.Integer pageview,
    java.math.BigDecimal score,
    java.util.Date createtime,
    java.util.Date updatetime) {

    super (
        topic,
        sectorname,
        contents,
        keyword,
        source,
        pageview,
        score,
        createtime,
        updatetime);
}

/*[CONSTRUCTOR MARKER END]*/

}

package test.base;

import java.io.Serializable;

/**

  • This is an object that contains data related to the ARTICLE table.
  • Do not modify this class because it will be overwritten if the configuration file
  • related to this class is modified. *
  • @hibernate.class
  • table="ARTICLE" */

public abstract class BaseArticle implements Serializable {

public static String REF = "Article";
public static String PROP_PAGEVIEW = "Pageview";
public static String PROP_KEYWORD = "Keyword";
public static String PROP_TOPIC = "Topic";
public static String PROP_UPDATETIME = "Updatetime";
public static String PROP_SCORE = "Score";
public static String PROP_CONTENTS = "Contents";
public static String PROP_CREATETIME = "Createtime";
public static String PROP_SOURCE = "Source";
public static String PROP_SECTORNAME = "Sectorname";


// constructors
public BaseArticle () {
    initialize();
}

/**
 * Constructor for primary key
 */
public BaseArticle (java.lang.String topic) {
    this.setTopic(topic);
    initialize();
}
/**
 * Constructor for required fields
 */
public BaseArticle (
    java.lang.String topic,
    java.lang.String sectorname,
    java.lang.String contents,
    java.lang.String keyword,
    java.lang.String source,
    java.lang.Integer pageview,
    java.math.BigDecimal  score,
    java.util.Date createtime,
    java.util.Date updatetime) {

    this.setTopic(topic);
    this.setSectorname(sectorname);
    this.setContents(contents);
    this.setKeyword(keyword);
    this.setSource(source);
    this.setPageview(pageview);
    this.setScore(score);
    this.setCreatetime(createtime);
    this.setUpdatetime(updatetime);
    initialize();
}

protected void initialize () {}



private int hashCode = Integer.MIN_VALUE;

// primary key
private java.lang.String topic;

// fields
private java.lang.String sectorname;
private java.lang.String contents;
private java.lang.String keyword;
private java.lang.String source;
private java.lang.Integer pageview;
private java.math.BigDecimal  score;
private java.util.Date createtime;
private java.util.Date updatetime;



/**
 * Return the unique identifier of this class
 * @hibernate.id
 *  generator-class="native"
 *  column="TOPIC"
 */
public java.lang.String getTopic () {
    return topic;
}

/**
 * Set the unique identifier of this class
 * @param topic the new ID
 */
public void setTopic (java.lang.String topic) {
    this.topic = topic;
    this.hashCode = Integer.MIN_VALUE;
}




/**
 * Return the value associated with the column: SECTORNAME
 */
public java.lang.String getSectorname () {
    return sectorname;
}

/**
 * Set the value related to the column: SECTORNAME
 * @param sectorname the SECTORNAME value
 */
public void setSectorname (java.lang.String sectorname) {
    this.sectorname = sectorname;
}



/**
 * Return the value associated with the column: CONTENTS
 */
public java.lang.String getContents () {
    return contents;
}

/**
 * Set the value related to the column: CONTENTS
 * @param contents the CONTENTS value
 */
public void setContents (java.lang.String contents) {
    this.contents = contents;
}



/**
 * Return the value associated with the column: KEYWORD
 */
public java.lang.String getKeyword () {
    return keyword;
}

/**
 * Set the value related to the column: KEYWORD
 * @param keyword the KEYWORD value
 */
public void setKeyword (java.lang.String keyword) {
    this.keyword = keyword;
}



/**
 * Return the value associated with the column: SOURCE
 */
public java.lang.String getSource () {
    return source;
}

/**
 * Set the value related to the column: SOURCE
 * @param source the SOURCE value
 */
public void setSource (java.lang.String source) {
    this.source = source;
}



/**
 * Return the value associated with the column: PAGEVIEW
 */
public java.lang.Integer getPageview () {
    return pageview;
}

/**
 * Set the value related to the column: PAGEVIEW
 * @param pageview the PAGEVIEW value
 */
public void setPageview (java.lang.Integer pageview) {
    this.pageview = pageview;
}



/**
 * Return the value associated with the column: SCORE
 */
public java.math.BigDecimal  getScore () {
    return score;
}

/**
 * Set the value related to the column: SCORE
 * @param score the SCORE value
 */
public void setScore (java.math.BigDecimal  score) {
    this.score = score;
}



/**
 * Return the value associated with the column: CREATETIME
 */
public java.util.Date getCreatetime () {
    return createtime;
}

/**
 * Set the value related to the column: CREATETIME
 * @param createtime the CREATETIME value
 */
public void setCreatetime (java.util.Date createtime) {
    this.createtime = createtime;
}



/**
 * Return the value associated with the column: UPDATETIME
 */
public java.util.Date getUpdatetime () {
    return updatetime;
}

/**
 * Set the value related to the column: UPDATETIME
 * @param updatetime the UPDATETIME value
 */
public void setUpdatetime (java.util.Date updatetime) {
    this.updatetime = updatetime;
}




public boolean equals (Object obj) {
    if (null == obj) return false;
    if (!(obj instanceof test.Article)) return false;
    else {
        test.Article article = (test.Article) obj;
        if (null == this.getTopic() || null == article.getTopic()) return false;
        else return (this.getTopic().equals(article.getTopic()));
    }
}

public int hashCode () {
    if (Integer.MIN_VALUE == this.hashCode) {
        if (null == this.getTopic()) return super.hashCode();
        else {
            String hashStr = this.getClass().getName() + ":" + this.getTopic().hashCode();
            this.hashCode = hashStr.hashCode();
        }
    }
    return this.hashCode;
}


public String toString () {
    return super.toString();
}

}

  • 写回答

1条回答 默认 最新

  • bohemia 2009-01-20 21:36
    关注

    就像你买了把牛刀,说"听说牛刀很牛,都能杀牛,结果我杀了只鸡,都这么费劲."

    呵呵.

    我对Hibernate不是很了解,但你说的这个只想获取2个字段;
    可以参照这个问题: http://www.iteye.com/problems/2257

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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