//Employee Table pojo class
public class Employee {
private String empno;
private String name;
private String job;
private int mgr;
private int sal;
private int comm;
private String deptno;
public void setEmpno(String empno){
this.empno=empno;
}
public void setName(String name){
this.name=name;
}
public void setJob(String job){
this.job=job;
}
public void setMgr(int mgr){
this.mgr=mgr;
}
public void setSal(int sal){
this.sal=sal;
}
public void setComm(int comm){
this.comm=comm;
}
public void setDeptno(String deptno){
this.deptno=deptno;
}
public String getDeptno(){
return this.deptno;
}
public int getComm(){
return this.comm;
}
public int getSal(){
return this.sal;
}
public int getMgr(){
return this.mgr;
}
public String getJob(){
return this.job;
}
public String getName(){
return this.name;
}
public String getEmpno(){
return this.empno;
}
}
// Department table pojo class
public class Department9 {
private String deptno;
private String dname;
private String loc;
//private int empno;
public void setDeptno(String 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;
}
public String getDeptno() {
return deptno;
}
}
// Mapping file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="emp9">
<id name="empno" type="string">
<column name="empno" sql-type="char(32)"/>
<generator class="uuid.hex" />
</id>
<many-to-one name="deptno" class="Department9"
column="deptno"
unique="true"
not-null="true" />
<property name="name" column="ename"/>
<property name="job" column="job"/>
<property name="mgr" column="mgr"/>
<property name="sal" column="sal"/>
<property name="comm" column="comm"/>
</class>
<class name="Department9" table="dept9" select-before-update="true">
<id name="deptno" type="string">
<column name="deptno" sql-type="char(32)"/>
<generator class="uuid.hex" />
</id>
<property name="dname" column="dname"/>
<property name="loc" column="loc"/>
</class>
</hibernate-mapping>
// Client code
mport org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class EmpDept {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Configuration cfg=new Configuration().configure();
System.out.println("Siva 1:::"+cfg);
SessionFactory sf=cfg.buildSessionFactory();
System.out.println("Siva 2");
Department9 dept = new Department9();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
dept.setDname("SALES");
dept.setLoc("Hyderabad");
session.save(dept);
System.out.println(dept.getDeptno());
Employee emp = new Employee();
emp.setComm(200);
emp.setDeptno(dept.getDeptno().toString());
emp.setJob("SALES");
emp.setMgr(5678);
emp.setName("Siva");
emp.setSal(10000);
session.save(emp);
tx.commit();
System.out.println("Siva 3");
session.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of Department9.deptno
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119)
at org.hibernate.tuple.AbstractTuplizer.getIdentifier(AbstractTuplizer.java:103)
at org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:2944)
at org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2705)
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:234)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at EmpDept.main(EmpDept.java:52)
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:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:105)
Hi, I am getting the above error, can you please help me any one and give me reply...
Thanks and Regards,
Siva Rama Krishna
|