Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi All, I'm having a problem while persisting an object which uses a reference.
I'm having two tables
1.Dept and
2.Emp
The database schema is...
Code:
Dept:
Deptno int(11) Pri
Dname varchar(255)
Loc varchar(255)
Code:
Emp:
Empno int(11) PRI
Ename varchar(255)
Job varchar(255)
Mgr int(11)
HireDate date
Sal float
Comm float
Deptno int(11) MUL References Dept(deptno)
The Mapping XML files are:
The corresponding DTD's are not given.
Dept.hbm.xml :Code:
<hibernate-mapping package="src.test">
<class name="Dept" table="dept">
<id name="deptno" column="Deptno" />
<property name="dname" column="Dname" />
<property name="loc" column="Loc" />
</class>
</hibernate-mapping>
<br><br>
Emp.hbm.xml:Code:
<hibernate-mapping package="src.test">
<class name="src.test.Emp" table="emp">
<id name="empno" column="Empno" />
<property name="ename" column="Ename" />
<property name="job" column="Job" />
<property name="mgr" column="Mgr" />
<property name="hiredate" column="HireDate" />
<property name="sal" column="Sal" />
<property name="comm" column="Comm" />
<many-to-one name="Deptno" class="src.test.Dept" column="DeptNo" not-null="false" />
</class>
</hibernate-mapping>
The Corresponding Pojo's are:
Dept.javaCode:
package src.test;
public class Dept{
private Integer deptno;
private String dname;
private String loc;
public Dept(){
//Default Constructor
}
public Integer getDeptno(){ return deptno; }
public void setDeptno(Integer deptno){
this.deptno=deptno;
}
public String getDname(){ return dname; }
public void setDname(String dname){
this.dname=dname;
}
public String getLoc(){ return loc; }
public void setLoc(String loc){
this.loc=loc;
}
}
Emp.javaCode:
package src.test;
import java.sql.Date;
public class Emp{
private Integer empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private float sal;
private float comm;
private Integer deptno;
public Emp(){
//Default Contructor
}
public Integer getEmpno(){ return empno; }
public void setEmpno(Integer empno){
this.empno=empno;
}
public String getEname(){ return ename; }
public void setEname(String ename){
this.ename=ename;
}
public String getJob(){ return job; }
public void setJob(String job){
this.job=job;
}
public int getMgr(){ return mgr; }
public void setMgr(int mgr){
this.mgr=mgr;
}
public Date getHiredate(){ return hiredate; }
public void setHiredate(Date hiredate){
this.hiredate=hiredate;
}
public float getSal(){ return sal; }
public void setSal(float sal){
this.sal=sal;
}
public float getComm(){ return comm; }
public void setComm(float comm){
this.comm=comm;
}
public Integer getDeptno(){ return deptno; }
public void setDeptno(Integer deptno){
this.deptno=deptno;
}
}
Hibernate version: 3.1.3
Mapping documents:Dept.hbm.xml and Emp.hbm.xml
Code between sessionFactory.openSession() and session.close():
session.beginTransaction();
Integer deptNo=new Integer(10);
emp.setEmpno(new Integer(1));
emp.setEname("Robin");
emp.setDeptno(deptNo);
emp.setComm(0.0f);
emp.setHiredate(new Date(04,04,04));
emp.setJob("Junior");
emp.setMgr(1);
emp.setSal(9200f);
session.saveOrUpdate(emp);
session.getTransaction().commit();
Full stack trace of any exception that occurs:
11:04:30,406 ERROR BasicPropertyAccessor:167 - IllegalArgumentException in class: src.test.Dept, getter method of property: deptno
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of src.test.Dept.deptno
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3257)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:2983)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:263)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:490)
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.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.saveOrUpdate(Unknown Source)
at classes.Transactions.insertIntoEmp(Transactions.java:256)
at classes.Transactions.insertRecord(Transactions.java:199)
at classes.Transactions.main(Transactions.java:37)
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(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:145)
... 26 more
Name and version of the database you are using:mysql-5.0.18
The generated SQL (show_sql=true):
select emp_.Empno, emp_.Ename as Ename1_, emp_.Job as Job1_, emp_.Mgr as Mgr1_, emp_.HireDate as HireDate1_, emp_.Sal as Sal1_, emp_.Comm as Comm1_, emp_.DeptNo as DeptNo1_ from emp emp_ where emp_.Empno=?
Debug level Hibernate log excerpt:
391 DEBUG IntegerType:80 - binding '1' to parameter: 1
I don't know why it is showing the error when i'm trying to persist an Emp object. But persisting an Dept object is working.
Please help me.
Any suggestions are appreciated.[/u]