-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Error while Persisting an Object
PostPosted: Tue May 23, 2006 2:21 am 
Newbie

Joined: Tue May 23, 2006 1:34 am
Posts: 19
Location: Bangalore
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.java
Code:
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.java
Code:
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]

_________________
Thanks and Regards,
Suresh Setty...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 5:36 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
use cascade ="save-update" on the association mapping for department in employee

<many-to-one name="Deptno" class="src.test.Dept" column="DeptNo" not-null="false" cascade="save-update"/>

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 6:31 am 
Newbie

Joined: Tue May 23, 2006 1:34 am
Posts: 19
Location: Bangalore
As i made that modification, it is still showing the same exception.
But thanks for your suggestion.

_________________
Thanks and Regards,
Suresh Setty...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 6:38 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
i noticed something in ur Emp class

the deptNo is supposed to be of type Dept

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;
(+) private Dept deptno ;

public Emp(){
//Default Contructor
}

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:00 am 
Newbie

Joined: Tue May 23, 2006 1:34 am
Posts: 19
Location: Bangalore
Thank you. Whether i have to make any changes, to the code while inserting i.e., in between creating and saving object in the session?

_________________
Thanks and Regards,
Suresh Setty...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:19 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
it can be somewhat like this

Code:
Emp emp = new Emp();

emp.setEmpno(new Integer(1));
emp.setEname("Robin");

//emp.setDeptno(deptNo);

Dept d = new Dept();
...
...
emp.setDeptno(d); // set the dept instance to be cascaded

emp.setComm(0.0f);
emp.setHiredate(new Date(04,04,04));
emp.setJob("Junior");
emp.setMgr(1);
emp.setSal(9200f);

session.saveOrUpdate(emp);

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:33 am 
Newbie

Joined: Tue May 23, 2006 1:34 am
Posts: 19
Location: Bangalore
Thank you Sherlin. Its working now. But i don't know why it is required? Can you explain me that?

Actually i'm a new member to the forums and i'm thinking that it may take day's to get response. But its not like that. Thanks again for your suggestions........:)

_________________
Thanks and Regards,
Suresh Setty...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:51 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
SureshSetty wrote:
But i don't know why it is required?


well,i think it has nothing to do with the hibernate , just part of object oriented design if u were asking about the Dept stuff .

if it was about the mapping , refer the documentation

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.