-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate newbie help
PostPosted: Mon Jun 27, 2005 7:02 am 
Newbie

Joined: Mon Jun 27, 2005 6:51 am
Posts: 2
Hi,

Im new with Hibernate and play around with it but I have a problem. I use hibernate 2.1 together with mySQL and I'm trying to CRUD several Objects. I have id columns in my tables which auto-increment. When i try to create and persist a TrainingGroup object, it works ok but when i try to do the same for an Employee, I get a "Employee altered from 1 to null" exception. My hbm.xml looks like this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="nl.jobs.businessobjects">
<class name="Employee" table="werknemer">

<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="firstname" column="Voornaam" />
<property name="lastname" column="Achternaam" />
<property name="direct" column="Direct" />
<property name="expertgroup_ref" column="ExpertiseGroep_id" />

</class>
</hibernate-mapping>

The SQL and exception followed are:

12:56:22,094 DEBUG SQL:230 - insert into werknemer (Voornaam, Achternaam, Direct, ExpertiseGroep_id) values (?, ?, ?, ?)

net.sf.hibernate.HibernateException: identifier of an instance of nl.jobs.businessobjects.Employee altered from 5 to null

at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2670)

at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2493)

at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2486)

at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2281)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)

at nl.jobs.util.HibernateUtil.createBusinessObject(HibernateUtil.java:100)

at nl.jobs.util.HibernateTestClient.createEmployee(HibernateTestClient.java:58)

at nl.jobs.util.HibernateTestClient.main(HibernateTestClient.java:29)

Can anyone help me out?

Much thanks,

Hugo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 8:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Nobody can help you without you posting the code you are executing.


Top
 Profile  
 
 Post subject: trying again
PostPosted: Tue Jul 05, 2005 3:55 am 
Newbie

Joined: Mon Jun 27, 2005 6:51 am
Posts: 2
ok,

I've changed a few things but ill try again. I want to save an employee to the db. My employee mapping file looks likes this:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="nl.jobs.businessobjects">
<class name="Employee" table="employee">

<id name="id" column="id">
  <generator class="increment"/>
</id>
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="direct" column="direct" />
<property name="nlnumber" column="nlnumber" />
<set name="expertsGroup" table="expertsgroup_employee">
  <key column="employee_id"/>
  <many-to-many column="expertsgroup_id" class="nl.jobs.businessobjects.ExpertsGroup"/>
</set>

</class>
</hibernate-mapping>


My expertsgroup mapping looks like this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="nl.jobs.businessobjects">
<class name="ExpertsGroup" table="expertsgroup">
<id name="id" column="id">
  <generator class="increment"/>
</id>
<property name="name" column="name" />
<set name="members" table="expertsgroup_employee">
  <key column="expertsgroup_id"/>
  <many-to-many column="employee_id" class="nl.jobs.businessobjects.Employee"/>
</set>
</class>
</hibernate-mapping>


The code for inserting a new employee looks like this:

Code:
    try
    {
      // create a new object
      Employee employee = new Employee();
      employee.setFirstname("One-punch");
      employee.setLastname("Mickey");
      employee.setDirect('J');
      java.util.Set xpgSet = new HashSet();
      xpgSet.add(new ExpertsGroup("Test XPG"));
      employee.setExpertsGroup(xpgSet);
      result = HibernateUtil.createBusinessObject(employee);
    }
    catch (HibernateException ex)
    {
      ex.printStackTrace();
    }


This result in :


net.sf.hibernate.HibernateException: identifier of an instance of nl.jobs.businessobjects.Employee altered from 7 to null

at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2670)

at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2493)

at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2486)

at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2281)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)

at nl.jobs.util.HibernateUtil.createBusinessObject(HibernateUtil.java:45)

at nl.jobs.util.HibernateTestClient.createEmployee(HibernateTestClient.java:99)

at nl.jobs.util.HibernateTestClient.main(HibernateTestClient.java:33)

Process exited with exit code 0.


This error also occurs once when i fech a list of employees and iterate over them.

Hopefully this is enough to give me a clue...

grtz
H[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.