-->
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: hbm.xml config for foreign key table
PostPosted: Wed May 26, 2010 6:00 am 
Newbie

Joined: Sun Apr 11, 2010 6:44 am
Posts: 12
Hi

I always get
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.jobscout.frontpage.client.Mesgtype.oid
Hibernate: update JOB set acctOid=?, description=?, catOid=?, subCatOid1=?, mesgtypeOid=? where oid=?
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.jobscout.frontpage.client.Mesgtype.oid

I am sure the oid getter is defined correctly. How to fix this problem?

I have a table, job, that has a foreign key
CREATE TABLE `job` (
`oid` int(10) NOT NULL AUTO_INCREMENT,
`acctOid` int(10) DEFAULT NULL,
`description` varchar(500) DEFAULT NULL,
`catOid` int(10) DEFAULT NULL,
`subCatOid1` int(10) DEFAULT NULL,
`mesgtypeOid` int(10) NOT NULL,
PRIMARY KEY (`oid`),
KEY `FK_job` (`mesgtypeOid`),
CONSTRAINT `FK_job` FOREIGN KEY (`mesgtypeOid`) REFERENCES `mesgtype` (`oid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `mesgtype` (
`oid` INT(10) NOT NULL,
`mesgtype` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=INNODB DEFAULT CHARSET=latin1

The following is the Job.hbm.xml

<class name="Job" table="JOB">
<id name="oid" type="long" column="oid" >
<generator class="increment"/>
</id>
<property name="acctOid" column="acctOid"/>
<property name="description" column="description"/>
<property name="catOid" column="catOid"/>
<property name="subCatOid1" column="subCatOid1"/>
<many-to-one class="Mesgtype" name="mesgtypeOid" column="mesgtypeOid" not-null="true"/>
</class>

Extract from Mesgtype.java
public class Mesgtype implements Serializable{
private long oid;
private String mesgtype;
public Mesgtype(){}

public Mesgtype(String _mesgtype)
{
mesgtype=_mesgtype;
}
public long getOid()
{
return this.oid;
}
Extract from Job.java
public class Job implements Serializable{
private long oid;
private long acctOid;
private String description;
private long catOid;
private long subCatOid1;
private long mesgtypeOid;

public Job(){}

public Job(long _acctOid, String _description, long _catOid, long _subCatOid1, long _mesgtypeOid)
{
acctOid=_acctOid;
description=_description;
catOid=_catOid;
subCatOid1=_subCatOid1;
mesgtypeOid=_mesgtypeOid;
}

public long getOid()
{
return oid;
}


Thanks


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Wed May 26, 2010 6:09 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What it your Job.getMesgtypeOid() method returning? I suspect it may be the value of the private long mesgtypeOid variable, but it must return a Mesgtype object since that is what you have told Hibernate in the <many-to-one class="Mesgtype" name="mesgtypeOid" ...> mapping.


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Thu May 27, 2010 3:53 am 
Newbie

Joined: Sun Apr 11, 2010 6:44 am
Posts: 12
Hi

Thank you very much for your reply.
I have print out the value of job.getMesgtypeOid() and it is a 0 as expected.
The most weird is that everytime the insert is run, a new foreign key is added to the table.
The exception occured at session.save(job).

Job job=new Job(_acctOid, _description, _catOid, _subCatOid1, _mesgtypeOid);
System.out.println("job.getMesgtypeOid()----> " + job.getMesgtypeOid());
try
{
if(!session.isOpen())
{
session = HibernateUtil.getSessionFactory().openSession();
}
session.beginTransaction();
session.save(job);//Exception occured
session.flush();
session.close();
//return success
return 0;
}
catch(Exception e)
{

---------------------------------------------------------------------------------------------------------

The table is defined as
CREATE TABLE `job` (
`oid` INT(10) NOT NULL AUTO_INCREMENT,
`acctOid` INT(10) DEFAULT NULL,
`description` VARCHAR(500) DEFAULT NULL,
`catOid` INT(10) DEFAULT NULL,
`subCatOid1` INT(10) DEFAULT NULL,
`mesgtypeOid` INT(10) NOT NULL,
PRIMARY KEY (`oid`),
KEY `FK_job` (`mesgtypeOid`),
CONSTRAINT `FK_job` FOREIGN KEY (`mesgtypeOid`) REFERENCES `mesgtype` (`oid`)
) ENGINE=INNODB DEFAULT CHARSET=latin1


After running the insert, a new FK is defined
CREATE TABLE `job` (
`oid` INT(10) NOT NULL AUTO_INCREMENT,
`acctOid` INT(10) DEFAULT NULL,
`description` VARCHAR(500) DEFAULT NULL,
`catOid` INT(10) DEFAULT NULL,
`subCatOid1` INT(10) DEFAULT NULL,
`mesgtypeOid` INT(10) NOT NULL,
PRIMARY KEY (`oid`),
KEY `FK11F9D732AB277` (`mesgtypeOid`),
CONSTRAINT `FK11F9D732AB277` FOREIGN KEY (`mesgtypeOid`) REFERENCES `mesgtype` (`oid`), CONSTRAINT `FK_job` FOREIGN KEY (`mesgtypeOid`) REFERENCES `mesgtype` (`oid`)
) ENGINE=INNODB DEFAULT CHARSET=latin1




Pls suggest some ways to fix it.
Thanks a lot.


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Thu May 27, 2010 4:43 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I have print out the value of job.getMesgtypeOid() and it is a 0 as expected.


This method should not return the ID value, it should return a Mesgtype object. Your code should be like:

Code:
private Mesgtype mesgtype;
public Mesgtype getMesgtypeOid()
{
  return mesgtype;
}
public void setMesgtypeOid(Mesgtype mesgtype)
{
  this.mesgtype = mesgtype;
}


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Fri May 28, 2010 5:25 am 
Newbie

Joined: Sun Apr 11, 2010 6:44 am
Posts: 12
Hi

Thank you for your reply. I have modified the Job class and there is no more error. However, when i insert the new Job, it is not saved to the table. I have verified from java DAO, the job can be retrieved. By executing sql from database, there is no result returned.

The following is the print out of the console:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into MESGTYPE (mesgtype, oid) values (?, ?)
Hibernate: insert into JOB (acctOid, description, catOid, subCatOid1, mesgtype, oid) values (?, ?, ?, ?, ?, ?)


Job.hbm.xml
<class name="Job" table="JOB">
<id name="oid" type="long" column="oid" >
<generator class="increment"/>
</id>
<property name="acctOid" column="acctOid"/>
<property name="description" column="description"/>
<property name="catOid" column="catOid"/>
<property name="subCatOid1" column="subCatOid1"/>
<many-to-one class="Mesgtype" name="mesgtype" cascade="all"/>

</class>



insert job

Job job=new Job(_acctOid, _description, _catOid, _subCatOid1, _mesgtype);
//System.out.println("job.getMesgtypeOid()----> " + job.getMesgtypeOid());
try
{
if(!session.isOpen())
{
session = HibernateUtil.getSessionFactory().openSession();
}
session.beginTransaction();
session.save(job);
session.flush();
session.close();
//return success
return 0;
}




Thanks


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Fri May 28, 2010 5:32 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
session.beginTransaction();
session.save(job);
session.flush();
session.close();


You never commit the transaction before closing the session. Everything that has been sent to the database will be rolled back. This is copied from the javadocs for the Session class (http://docs.jboss.org/hibernate/stable/ ... ssion.html):

Code:
A typical transaction should use the following idiom:
Session sess = factory.openSession();
Transaction tx;
try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
}
catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
}
finally {
     sess.close();
}


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Mon May 31, 2010 5:29 am 
Newbie

Joined: Sun Apr 11, 2010 6:44 am
Posts: 12
Hi

Job.hbm.xml

<many-to-one class="Mesgtype" name="mesgtype" cascade="all"/>

Job.java
private Mesgtype mesgtype; <---foreign key
public Mesgtype getMesgtype()
{
return this.mesgtype;
}
public void setMesgtype(Mesgtype _mesgtype)
{
mesgtype=_mesgtype;
}


According to my config, the constraint is expected to be installed by the hibernate. Therefore, when the insert with foreign key index out of range, the insert will not be successed. In my situation, everytime the job is inserted, a new mesgtype is also inserted. Obviously, the foreign key constraint is not installed right. I tried to remove the job table, and I let the hibernate to create the table and the constraint with the mesgtype table. The table info shows the create statement:

CREATE TABLE `job` (
`oid` bigint(20) NOT NULL AUTO_INCREMENT,
`acctOid` bigint(20) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`catOid` bigint(20) DEFAULT NULL,
`subCatOid1` bigint(20) DEFAULT NULL,
`mesgtype` bigint(20) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `FK11F9D5D27BCB9` (`mesgtype`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1


Obviously, the constraint is not installed. How should I fix it?

Thanks


Top
 Profile  
 
 Post subject: Re: hbm.xml config for foreign key table
PostPosted: Mon May 31, 2010 5:32 am 
Newbie

Joined: Sun Apr 11, 2010 6:44 am
Posts: 12
Hi

Job.hbm.xml

<many-to-one class="Mesgtype" name="mesgtype" cascade="all"/>

Job.java
private Mesgtype mesgtype; <---foreign key
public Mesgtype getMesgtype()
{
return this.mesgtype;
}
public void setMesgtype(Mesgtype _mesgtype)
{
mesgtype=_mesgtype;
}


According to my config, the constraint is expected to be installed by the hibernate. Therefore, when the insert with foreign key index out of range, the insert will not be successed. In my situation, everytime the job is inserted, a new mesgtype is also inserted. Obviously, the foreign key constraint is not installed right. I tried to remove the job table, and I let the hibernate to create the table and the constraint with the mesgtype table. The table info shows the create statement:

CREATE TABLE `job` (
`oid` bigint(20) NOT NULL AUTO_INCREMENT,
`acctOid` bigint(20) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`catOid` bigint(20) DEFAULT NULL,
`subCatOid1` bigint(20) DEFAULT NULL,
`mesgtype` bigint(20) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `FK11F9D5D27BCB9` (`mesgtype`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1


Obviously, the constraint is not installed. How should I fix it?

Thanks


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.