为什么这个错误java.lang.IllegalArgumentException:对象不是声明类的实例,在代码中显示

时间:2017-03-11 18:56:41

标签: java hibernate

问题:

GivenProblemStatement

1)定义实体教师。教师可以教授许多科目。

2)受试者有另一个受试者作为父母受试者。

3)教师使用复合键(名称,主题)。

解决方案:

我已经实现了以上几点,我正在努力保存细节 在数据库中使用HibernateTemplate的Teacher类Object以及父类对象。 但是,我无法保存父类对象。 的原因: 由于下面显示的错误

错误:

Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred calling getter of str.Parent.subjectId; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of str.Parent.subjectId
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at dao.EmployeeDao.saveEmployee(EmployeeDao.java:23)
at str.A.abc(A.java:26)
at str.OurLogic.main(OurLogic.java:56)
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of str.Parent.subjectId
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:198)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:227)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3850)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3558)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:120)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 5 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172)
... 23 more

代码

OurLogic.java

Below is my code:
package str;
public class OurLogic {

public static void main(String args[])
{
Teacher t =new Teacher();
t.setTeacherId(101);
t.setTeacherName("Jalaj");

//creating 3 child objects
Subject s1=new Subject();

s1.setSubjectId(504);
s1.setSubjectName("JSE");

Subject s2=new Subject();

s2.setSubjectId(505);
s2.setSubjectName("JEE");

Subject s3=new Subject();

s3.setSubjectId(506);
s3.setSubjectName("Spring");
Parent p=new Parent();
p.setParentSubjectName("Java"); 
p.setS(s1);
// adding child objects to set, as we taken 3rd property set in parent
Set s=new HashSet();

s.add(s1);
s.add(s2);
s.add(s3);

t.setSubjects(s);

String subjectName="JEE";//Given a subjectN
A.abc(t,p,subjectName,s);
}

A.java

package str;
public class A {  
public static void abc(Teacher t,Parent p,String s1,Set s) {  
ApplicationContext bfact=new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDao dao=(EmployeeDao)bfact.getBean("d");  


//For insert
dao.saveEmployee(t);  
dao.saveEmployee(p);
}

EmployeeDao

package dao;
public class EmployeeDao {  
HibernateTemplate template;  
public void setTemplate(HibernateTemplate template) {  
this.template = template;  
}  
//method to save employee  
public void saveEmployee(Teacher t){  
template.save(t);  
}  
public void saveEmployee(Parent p){  
template.save(p);  
} 

Parent.java

public class Parent { 
private int subjectId;
private String parentSubjectName;
private Subject s;
public Subject getS() {
    return s;
}
public void setS(Subject s) {
    this.s = s;
}
public int getSubjectId() {
    return subjectId;
}
public void setSubjectId(int subjectId) {
    this.subjectId = subjectId;
}
public String getParentSubjectName() {
    return parentSubjectName;
}
public void setParentSubjectName(String parentSubjectName) {
    this.parentSubjectName = parentSubjectName;
}   
}

Subject.java

public class Subject implements Serializable{
private int subjectId;
private String subjectName;
private int forevenId;
public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public int getForevenId() {
return forevenId;
}
public void setForevenId(int forevenId) {
this.forevenId = forevenId;
}
}

Teacher.java

public class Teacher implements Serializable{
private int teacherId;
private String teacherName;
private Set subjects;
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Set getSubjects() {
return subjects;
}
public void setSubjects(Set subjects) {
this.subjects = subjects;
} 
}

的applicationContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
<property name="driverClassName"  value="com.mysql.jdbc.Driver">  </property>  
<property name="url" value="jdbc:mysql://localhost:3306/JalajSchema"> </property>  
<property name="username" value="root"></property>  
<property name="password" value=""></property>  
</bean>     
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
<property name="dataSource" ref="dataSource"></property>          
<property name="mappingResources">  
<list>  
<value>Teacher.hbm.xml</value>           
<value>Subject.hbm.xml</value>   
<value>Parent.hbm.xml</value>  
</list>  
</property>            
<property name="hibernateProperties">  
<props>                <propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
<prop key="hibernate.hbm2ddl.auto">create</prop>  
<prop key="hibernate.show_sql">true</prop>          
</props>  
</property>  
</bean>      
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
<property name="sessionFactory" ref="mysessionFactory"></property>  
</bean>       
<bean id="d" class="dao.EmployeeDao">  
<property name="template" ref="template"></property>  
</bean>       
</beans>  

Subject.hbm.xml

<hibernate-mapping>
<class name="str.Subject" table="subject"> 
<id name="subjectId" column="subjid"  />
<property name="subjectName" column="subjname" length="10"/>
<property name="forevenId" column="forevenid"  insert="false" />
</class>
</hibernate-mapping>

Teacher.hbm.xml

<hibernate-mapping>
<class name="str.Teacher" table="Teacher1">
<id name="teacherId" column="teachid"  /> 
<property name="teacherName" column="tname" length="10"/>
<set name="subjects" cascade="all" >
<key column="forevenid" />
<one-to-many class="str.Subject" />
</set>
</class>
</hibernate-mapping>

Parent.hbm.xml

<hibernate-mapping>
<class name="str.Parent" table="Parent">
<id name="subjectId" column="subjectId" >
<generator class="foreign">  
<param name="property">s</param>
</generator>  
</id>  
<property name="parentSubjectName" column="parentSubjectName" length="10"/>
<one-to-one name="s" class="str.Parent" cascade="all" />
</class>
</hibernate-mapping>
  

任何人都可以指导我如何纠正它吗?

0 个答案:

没有答案