如何解决“调用属性的setter时发生IllegalArgumentException”?

时间:2015-07-27 05:26:33

标签: java hibernate hibernate-mapping hibernate-criteria

我正在尝试使用条件

从Messages表中获取所有条目
Criteria getCriteria = session.createCriteria(MessageAttributes.class);
List<MessageAttributes> unProcessedJcbMessagesList=getCriteria.list(); 

当我尝试运行超出标准时,我正在跟踪异常。 FieldId是AvailableFields(AvailableFieldsIs)表中的外键。

  IllegalArgumentException occurred while calling setter for property [com.alu.jcb.model.MessageAttributes.fieldId (expected type = com.alu.jcb.model.AvailableFields)]; target = [com.alu.jcb.model.MessageAttributes@1cf3877], property value = [2]
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:123)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:713)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:362)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4718)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:188)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1718)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    at com.alu.jcb.messageProcessor.Test.main(Test.java:23)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:68)
    ... 17 more

MessageAttributes.java

public class MessageAttributes {
    private long messageAttributeId=1L;
    private AvailableFields fieldId;
    private String value;
//getters and setters
}

AvailableFields.java

public class AvailableFields {

    private long availableFieldsId=1L;
    private Instance instance;
    private String fieldDisplayName;

//getters and setters
}

HBM文件 MessageAttributes.hbm.xml

<hibernate-mapping package="com.alu.jcb.model">
    <class name="MessageAttributes" table="MESSAGE_ATTRIBUTES">
        <id name="messageAttributeId" column="message_attribute_id" type="long">
            <generator class="sequence" />
        </id>

        <many-to-one name="fieldId" class="com.alu.jcb.model.AvailableFields" fetch="select">
            <column name="field_id" not-null="true" />
        </many-to-one>

        <property name="value" type="string" column="value" />                          
    </class>
</hibernate-mapping>

AvailableFields.hbm.xml

<hibernate-mapping package="com.alu.jcb.model">
    <class name="AvailableFields" table="AVAILABLE_FIELDS">
         <id name="availableFieldsId" column="available_fields_id" type="java.lang.Long">
            <generator class="sequence" />
        </id>
        <many-to-one name="instance" class="com.alu.jcb.model.Instance" fetch="select">
            <column name="instance_id" not-null="true" />
        </many-to-one>
        <property name="fieldDisplayName" type="string" column="field_display_name" not-null="true"/>
    </class>
</hibernate-mapping>

请帮帮我

1 个答案:

答案 0 :(得分:1)

错误意味着fieldId的setter出错了。它应该是这样的

public void setFieldId(AvailableFields fieldId) {
   this.fieldId=fieldId;
}

但似乎在你的情况下,在setter中使用了不正确的类(guess long)。