web-dev-qa-db-ja.com

Hibernate-クラスのgetterを呼び出してIllegalArgumentExceptionが発生しました

このエラーが発生しているようですが、なぜこのエラーが発生するのかわかりません。とにかくそのクラスで直接アクションを実行することは想定されていないため、HibernateがUserクラスにそのゲッターを必要とする理由さえ理解していません...

2012-12-30 09:38:56,713 [main]エラーorg.hibernate.property.BasicPropertyAccessor $ BasicGetter-HHH000122:クラスのIllegalArgumentException:com.nortal.pirs.datamodel.User、プロパティのゲッターメソッド:idUser 2012-12- 30 09:38:56,714 [main]エラーcom.nortal.pirs.businesslogic.logic.VisitManagerLogic-com.nortal.pirs.datamodel.User.idUser Java.lang.IllegalArgumentExceptionのゲッターを呼び出すとIllegalArgumentExceptionが発生しました:オブジェクトはのインスタンスではありませんクラスの宣言

完全なスタックトレースは次のようになります。

    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を持つVisitを追加しようとしたときに発生します。このフィールドは、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;
    }

}

私のUserMapperxml:

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

そして私のVisitMapperxml :(これは実際に動作しているはずです。Hibernateがユーザー用のゲッターを必要とする理由さえ理解していません...)

<?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" />

誰かがこれを手伝ってくれることを願っています。ありがとう

11
Arturas M

あなたは、VisitクラスがPatientクラス(および他の2つのクラス)と多対1の関連があることをHibernateに伝えています。そのような関連はありません。あなたがそのような暗示を持っていた場合、Visitクラスには

private Patient patient;

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

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

代わりに、Visitエンティティにあるのは患者の[〜#〜] id [〜#〜]です。それは関連を構成しません。

Hibernateドキュメント アソシエーションについて読んでください。アソシエーションが何であるか、およびORMが実際に許可すること、つまりオブジェクトのグラフをロード、ナビゲート、およびクエリできることを見逃しているためです。

17
JB Nizet

このエラーは、間違ったパラメータバインディング方法を使用した場合にも発生する可能性があります。例えば:

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

使用する

query.setParameter("clients", clientList);

の代わりに

query.setParameterList("clients", clientList);

上記のエラーが発生します。

2
isah