Hibernate - 调用类的getter时发生IllegalArgumentException

时间:2012-12-30 08:06:04

标签: hibernate illegalargumentexception

我似乎遇到了这个错误,并且不知道为什么我会收到此错误。我甚至不明白为什么Hibernate需要User类的getter,因为它不应该直接对该类进行任何操作...

  

2012-12-30 09:38:56,713 [main] ERROR org.hibernate.property.BasicPropertyAccessor $ BasicGetter - HHH000122:类中的IllegalArgumentException:com.nortal.pirs.datamodel.User,属性的getter方法:idUser   2012-12-30 09:38:56,714 [main] ERROR com.nortal.pirs.businesslogic.logic.VisitManagerLogic - 调用getter of com.nortal.pirs.datamodel.User.idUser时发生IllegalArgumentException   java.lang.IllegalArgumentException:object不是声明类

的实例

完整的堆栈跟踪如下所示:

    Hibernate: select max(idVisit) from Visit
2012-12-30 09:38:56,713 [main] ERROR org.hibernate.property.BasicPropertyAccessor$BasicGetter - HHH000122: IllegalArgumentException in class: com.nortal.pirs.datamodel.User, getter method of property: idUser
2012-12-30 09:38:56,714 [main] ERROR com.nortal.pirs.businesslogic.logic.VisitManagerLogic - IllegalArgumentException occurred calling getter of com.nortal.pirs.datamodel.User.idUser
java.lang.IllegalArgumentException: object is not an instance of declaring class
    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$BasicGetter.get(BasicPropertyAccessor.java:164)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:341)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4491)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4213)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:209)
    at org.hibernate.engine.internal.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:165)
    at org.hibernate.engine.internal.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:94)
    at org.hibernate.engine.internal.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:72)
    at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:128)
    at org.hibernate.action.internal.AbstractEntityInsertAction.makeEntityManaged(AbstractEntityInsertAction.java:139)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:209)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:136)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:328)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:136)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:756)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:748)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:744)
    at com.nortal.pirs.persistence.dbhibernate.visits.VisitDaoHibernate.addVisit(VisitDaoHibernate.java:46)
    at com.nortal.pirs.businesslogic.logic.VisitManagerLogic.addVisit(VisitManagerLogic.java:39)
    at com.nortal.pirs.test.persistence.filldata.FillVisits.fill(FillVisits.java:31)
    at com.nortal.pirs.test.persistence.filldata.FillVisits.main(FillVisits.java:21)

当尝试添加一个具有字段idPatient的访问时出现此错误,该字段为long类型,并且应该是访问正在注册的患者的id,所以我得到的方法这看起来像这样(类VisitDaoHibernate):

@Override
    public void addVisit(Visit visit) {
        session = connection.getSession();
        session.beginTransaction();
        session.save(visit); // here it happens...
        session.getTransaction().commit();
    }

访问班:

    /**
 * @(#) Visit.java
 */

package com.nortal.pirs.datamodel;

import java.util.Date;

import com.nortal.pirs.datamodel.enumeration.VisitState;

public class Visit {
    private long idVisit;
    private long idPatient;
    private long idSpiProfessional;
    private long idProfession;
    private Date visitDate = new Date();
    private String reason = "";
    private VisitState visitState = VisitState.REGISTERED;


    /**
     * @param patient
     * @param spiProfessional
     * @param visitDate
     * @param reason
     * @param visitState
     * @param visitProfession
     */
    public Visit() {
    }   

    public Visit(Visit visit) {
        this.idPatient = visit.getIdPatient();
        this.idSpiProfessional = visit.getIdSpiProfessional();
        this.idProfession = visit.getIdProfession();
        this.visitDate = visit.getVisitDate();
        this.reason = visit.getReason();
        this.visitState = visit.getVisitState();
    }

    public long getIdVisit() {
        return idVisit;
    }

    public void setIdVisit(long idVisit) {
        this.idVisit = idVisit;
    }

    public long getIdPatient() {
        return idPatient;
    }

    public void setIdPatient(long idPatient) {
        this.idPatient = idPatient;
    }

    public long getIdSpiProfessional() {
        return idSpiProfessional;
    }

    public void setIdSpiProfessional(long idSpiProfessional) {
        this.idSpiProfessional = idSpiProfessional;
    }

    public long getIdProfession() {
        return idProfession;
    }

    public void setIdProfession(long idProfession) {
        this.idProfession = idProfession;
    }

    public Date getVisitDate() {
        return visitDate;
    }

    public void setVisitDate(Date visitDate) {
        this.visitDate = visitDate;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public VisitState getVisitState() {
        return visitState;
    }

    public void setVisitState(VisitState visitState) {
        this.visitState = visitState;
    }
}

我的用户类:

    /**
 * @(#) User.java
 */

package com.nortal.pirs.datamodel;

import java.util.Date;

import com.nortal.pirs.datamodel.enumeration.Gender;
import com.nortal.pirs.datamodel.enumeration.UserState;

public class User {
    private long idUser;
    private String firstName = "";
    private String lastName = "";
    private String personCode = "";
    private Date birthDate = new Date();
    private Gender gender = Gender.MALE;
    private String email = "";
    private String password = "";
    private UserState userState = UserState.UNAPPROVED;

    public User(User user) {
        idUser = user.getIdUser();
        firstName = user.getFirstName();
        lastName = user.getLastName();
        personCode = user.getPersonCode();
        birthDate = user.getBirthDate();
        gender = user.getGender();
        email = user.getEmail();
        password = user.getPassword();
        userState = user.getUserState();
    }

    public User() {

    }

    public boolean sameAs(User user) {
        if (user == null) {
            return false;
        }

        if (this.getEmail().equals(user.getEmail())
                && this.getBirthDate().equals(user.getBirthDate())
                && this.getFirstName().equals(user.getFirstName())
                && this.getLastName().equals(user.getLastName())
                && this.getGender().equals(user.getGender())
                && this.getPassword().equals(user.getPassword())
                && this.getPersonCode().equals(user.getPersonCode())
                && this.getUserState().equals(user.getUserState())) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * @param firstName
     * @param lastName
     * @param personCode
     * @param birthDate
     * @param gender
     * @param email
     * @param password
     * @param userState
     */
    public User(String firstName, String lastName, String personCode,
            Date birthDate, Gender gender, String email, String password,
            UserState userState) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.personCode = personCode;
        this.birthDate = birthDate;
        this.gender = gender;
        this.email = email;
        this.password = password;
        this.userState = userState;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPersonCode() {
        return personCode;
    }

    public void setPersonCode(String personCode) {
        this.personCode = personCode;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserState getUserState() {
        return userState;
    }

    public void setUserState(UserState userState) {
        this.userState = userState;
    }

    public long getIdUser() {
        return idUser;
    }

    public void setIdUser(long idUser) {
        this.idUser = idUser;
    }

}

我的UserMapper xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nortal.pirs.datamodel">

    <class name="User" table="User">

        <id name="idUser" type="long" column="idUser">
            <generator class="increment"/>
        </id>

        <property name="firstName" />
        <property name="lastName" />
        <property name="personCode" />
        <property name="birthDate" type="date"/>
        <property name="email" />
        <property name="password" />

       <property name="userState" column="userState" length="15">
             <type name="org.hibernate.type.EnumType">
                  <param name="enumClass">com.nortal.pirs.datamodel.enumeration.UserState</param>
                  <param name="type">12</param>
              </type>
        </property>

         <property name="gender" column="gender" length="15">
             <type name="org.hibernate.type.EnumType">
                  <param name="enumClass">com.nortal.pirs.datamodel.enumeration.Gender</param>
                  <param name="type">12</param>
              </type>
        </property>

         <joined-subclass name="Patient" table="Patient">
             <key column="idUser" />
             <property name="additionalInfo" column="additionalInfo" />
         </joined-subclass>

         <joined-subclass name="SpiProfessional" table="SpiProfessional">
                 <key column="idUser" />
         </joined-subclass>

    </class>

</hibernate-mapping>

我的VisitMapper xml :(这是一个,实际应该在行动中,我甚至不理解为什么Hibernate需要用户的getter ...)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nortal.pirs.datamodel">

    <class name="Visit"
        table="Visit">

        <id name="idVisit">
            <generator class="increment"/>
        </id>

        <property name="visitDate" type="date" />
        <property name="reason" />

       <property name="visitState" column="visitState" length="15">
             <type name="org.hibernate.type.EnumType">
                  <param name="enumClass">com.nortal.pirs.datamodel.enumeration.VisitState</param>
                  <param name="type">12</param>
              </type>
        </property>

        <many-to-one name="idPatient" not-null="false" class="com.nortal.pirs.datamodel.Patient"/>
        <many-to-one name="idSpiProfessional" not-null="false" class="com.nortal.pirs.datamodel.SpiProfessional" />
        <many-to-one name="idProfession" not-null="false" class="com.nortal.pirs.datamodel.Profession" />

我希望有人可以帮助我。感谢

3 个答案:

答案 0 :(得分:17)

你告诉Hibernate,Visit类与Patient类(以及另外两个类)有多对一的关联。它没有这样的联想。如果你有这样的联系,那么Visit类将包含

private Patient patient;

public Patient getPatient() {
    return this.patient;
}

public void setPatient(Patient patient) {
    this.patient = patient;
}

相反,您访问实体中的内容是患者的 ID 。这不构成关联。

阅读关于关联的Hibernate documentation,因为你已经错过了它们,以及ORM实际允许的大量内容,即能够加载,导航和查询对象的图形。

答案 1 :(得分:1)

使用错误的参数绑定方法时也会发生此错误。例如:

class QuestionList(generics.ListAPIView, generics.CreateAPIView):
    queryset = Question.objects.all()
    serializer_class = QuestionSerializer

使用

select o from Order o where o.client in (:clients)

代替

query.setParameter("clients", clientList);

将导致上述错误。

答案 2 :(得分:0)

在我的(我假设,很少见)的情况下,类似的错误是由于该字段造成的:

    @MapKey
    public Map<Integer, AttributeValue_> getAttrValues() {
        return attrValues;
    }

问题在于 AttributeValue 主键不是整数。因此,ClassCastException 导致 IllegalArgumentException。

解决方案是制作

    @MapKeyColumn(name = "attr_id")

将地图的键绑定到某个属性 attr_id。