1

我将此类 BidToolTradeLanes 定义如下:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.catapult.bid.model;

/**
 *
 * @author Alok Shrestha
 */
public class BidToolTradeLanes {
    private int tradeLaneId;
    private String tradeLaneName;
    private int contractId;

public BidToolTradeLanes() {
}
public int getContractId() {
    return contractId;
}

public void setContractId(int contractId) {
    this.contractId = contractId;
}

public int getTradeLaneId() {
    return tradeLaneId;
}

public void setTradeLaneId(int tradeLaneId) {
    this.tradeLaneId = tradeLaneId;
}

public String getTradeLaneName() {
    return tradeLaneName;
}

}

而且,我有上述类的映射文件:

<hibernate-mapping package="com.catapult.bid.model"  default-access="field"> 
<class name="BidToolTradeLanes" table="bt_trade_lane" schema="bidtool" >
    <id name="tradeLaneId" type="int" column="trade_lane_id">           
        <generator class="sequence">
             <param name="sequence">bidtool.trade_lane_trade_lane_id_seq</param>
        </generator>
    </id>  

    <property name="tradeLaneName" type="string">
        <column name="tradelane_name" length="20"/>
    </property>      
     <many-to-one class="Contracts" fetch="select" name="contractId">
         <column name="contract_id"/>
    </many-to-one>
</class>

现在,每当我尝试运行此语句时,我都会收到错误消息

List list=session.createSQLQuery("select {t.*} from bidtool.bt_trade_lane t")
                  .addEntity("t",BidToolTradeLanes.class).list();

我得到的错误是:

could not set a field value by reflection setter of com.catapult.bid.model.BidToolTradeLanes.contractId

您的帮助将不胜感激。

4

1 回答 1

2

Since contractId is mapped as <many-to-one> its type should be Contracts, not int.

That's the difference between database schema and object model: in your database schema you have foreign keys, whereas in the object model you have references to other objects.

Also note that contract as a name for the reference would be better than contractId, and that classes in object model are usually named in singular, not in plural.

See also:

于 2012-05-29T16:48:53.720 回答