问题
I seem to be getting this error and have no idea why I'm getting this error. I don't even understand why Hibernate needs that getter for the User class because it's not supposed to be doing any actions with that class directly anyway...
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
The full stacktrace looks like this:
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)
This error occurs when trying to add a Visit, which has a field idPatient, which is of type long and is supposed to be the id of the patient that the visit is being registered for, so the method where I'm getting this at looks like this (class VisitDaoHibernate):
@Override
public void addVisit(Visit visit) {
session = connection.getSession();
session.beginTransaction();
session.save(visit); // here it happens...
session.getTransaction().commit();
}
The Visit class:
/**
* @(#) 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;
}
}
My User class:
/**
* @(#) 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;
}
}
My 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>
And my VisitMapper xml: (this is the one, that should be in action actually, I don't even understand why Hibernate needs the getter for user at all...)
<?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" />
I just hope somebody can help me with this. Thanks
回答1:
You're telling Hibernate that the Visit class has a many-to-one association with the Patient class (and with two other classes). It doesn't have such an association. If you had such an assciation, the Visit class would contain
private Patient patient;
public Patient getPatient() {
return this.patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
Instead, what you have in your Visit entity is the ID of a patient. That doesn't constitute an association.
Read the Hibernate documentation about associations, because you've missed what they are, and a great deal of what an ORM actually allows, which is to be able to load, navigate and query graphs of objects.
回答2:
This error can also occur when using the wrong parameter binding method. For example:
select o from Order o where o.client in (:clients)
Using
query.setParameter("clients", clientList);
instead of
query.setParameterList("clients", clientList);
would result in the above error.
来源:https://stackoverflow.com/questions/14089233/hibernate-illegalargumentexception-occurred-calling-getter-of-a-class