Alok
Alok

Reputation: 244

could not set a field value by reflection setter error in hibernate

I have this class BidToolTradeLanes defined as follows:

/*
 * 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;
}

}

And, I have this mapping file for the above class:

<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>

Now, whenever I try to run this statement, I get the error

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

I get the error as:

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

Your help will be appreciated.

Upvotes: 1

Views: 6450

Answers (1)

axtavt
axtavt

Reputation: 242686

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:

Upvotes: 2

Related Questions