0

Problem:

GivenProblemStatement:

1)Define en Entity Teacher.A Teacher can teach many Subjects.

2)Subject have another Subject as Parent Subject.

3)Teacher uses a Composite key(name,subject).

Solution:

I have implemented the above points and I am trying to save the details in the database using HibernateTemplate of Teacher class Object as well as parent class object. But,I am not able to save the parent class object. Reason: Because of the error shown below


Error:

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

Code

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>

Can anyone guide me how to correct it?

8
  • Don't post images, post code. Also, regarding Set s=new HashSet();, do not use raw types! Commented Mar 11, 2017 at 19:11
  • The problem is in code you haven't posted, but the message tells you that you provided an argument to a getter method for str.Parent.subjectId that is not of the correct type. What is str? (You need to use better variable names.) What is Parent? Too much is missing from your question. Commented Mar 11, 2017 at 19:20
  • @LewBloch Have Edited the Code.Parent is a class and str is a package.Will keep in mind the details you mentioned Commented Mar 12, 2017 at 6:52
  • @LewBloch "but the message tells you that you provided an argument to a getter method for str.Parent.subjectId" I have not understood your statement.I have earlier used the Hibernate code and it was working fine but with Hibernate Template it is not working. Commented Mar 12, 2017 at 6:55
  • References Commented Mar 12, 2017 at 6:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.