我正在尝试使用条件从“消息”表中获取所有条目

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>


请帮我

最佳答案

该错误表示fieldId的setter出了点问题。应该是这样

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


但在您的情况下,似乎在二传手中使用了错误的类(猜测很长)。

10-07 12:03