hibernate 如何解决“调用属性的setter时出现IllegalArgumentException”?

x9ybnkn6  于 2022-11-24  发布在  其他
关注(0)|答案(3)|浏览(138)

我正在尝试使用条件从Messages表中获取所有条目

Criteria getCriteria = session.createCriteria(MessageAttributes.class);
List<MessageAttributes> unProcessedJcbMessagesList=getCriteria.list();

尝试运行上述条件时出现以下异常。FieldId是AvailableFields(AvailableFielddsIs)表中的外键。

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

豆类
消息属性.java**

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

可用字段.java

public class AvailableFields {

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

//getters and setters
}

HBM文件****消息属性.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>

可用字段. 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>

请帮帮我

falq053o

falq053o1#

这个错误说明fieldId的setter有问题,应该是这样的

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

但在您的例子中,setter中使用了不正确的类(猜测长)。

xqk2d5yq

xqk2d5yq2#

查询结果为:
如果您有任何问题,请联系我们。

1l5u6lss

1l5u6lss3#

我收到这个错误
调用属性Version的setter时发生IllegalArgumentException(类型应为Long,但收到0)
因为我们不用

Query query = sess.createSQLQuery(sqlQueryStr).addEntity(instance.getClass().getName());
retval = query.list();

我在做

Query query = sess.createSQLQuery(sqlQueryStr)
        .setResultTransformer(Transformers.aliasToBean(instance.getClass()));
    retval = query.list();

以防对任何人有帮助。

相关问题