-->
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: mapping simple java inheritance in Hibernate
PostPosted: Mon Apr 10, 2006 1:40 pm 
Newbie

Joined: Wed Mar 22, 2006 4:46 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Ok, I am trying to map a simple inheritance java model into a hibernate application. I have a superclass of Person and two subclasses (ServiceMember and Dependent). I can insert a simple Person object with no problems but when I add on the inheritance it gives me errors noted below. I am using Apache Ant to build and insert the data. Also, I am letting Hibernate create and drop the tables initially to prevent any possible confilict in datatypes. I believe that my problem lies in the mapping file for the inheritance relationship. I am currently trying to use the table per subclass method discussed in the documentation. ( I appologize for lack of formatting on the code and xml here)

Hibernate version:
3.1.3
Mapping documents:

<?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="person.Person" table="per">
<id name="id" column="per_id">
<generator class="native"/>
</id>
<property name="firstname"/>
<property name="lastname"/>
<property name="SSN"/>
<property name="gender"/>
<property name="bday"/>
<property name="address"/>
<property name="city"/>
<property name="zip"/>
<joined-subclass name="person.ServiceMember" table="sm">
<key column="sm_id"/>
<property name="rank"/>
<property name="branch"/>
<property name="nok"/>
<property name="deployed"/>
</joined-subclass>
<joined-subclass name="person.Dependent" table="dep">
<key column="dep_id"/>
<property name="relationship"/>
</joined-subclass>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
package person;

import java.util.*;
import util.HiberateUtil;

public class DataManager {

public static void main(String[] args) {

DataManager mgr = new DataManager();

if (args[0].equals("list")) {

List serviceMembers = mgr.listServiceMembers();

for (int i = 0; i < serviceMembers.size(); i++) {

ServiceMember aServiceMember = (ServiceMember) serviceMembers.get(i);

System.out.println("Servicemember: " + aServiceMember.getLastname());
}

}else if(args[0].equals("store")){
mgr.storeServiceMember("LT", "NoName");
}

HibernateUtil.getSessionFactory().close();
}

private List listServiceMembers(){

Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

List result = session.createQuery("from ServiceMember").list();

session.getTransaction().commit();

return result;
}

private void storeServiceMember(String rank, String lastname){

Session s = HibernateUtil.getSessionFactory().getCurrentSession();

ServiceMember sm = new ServiceMember();
sm.setRank(rank);
sm.setLastname(lastname);

s.save(sm);

s.getTransaction().commit();
}

}


Full stack trace of any exception that occurs:

I am getting the following error during the run phase:

Initial SessionFactory creation failed.org.hibernate.PropertyAccessException: IllegalArguementException occurred calling getter of person.Person.id

and further it states that it is caused by IllegalArgumentException calling the getter of person.Person.id.

I tested this without the inheritance and I can put Person objects into the DB without any issues it is only when I try to model the inheritance that it fails.

Name and version of the database you are using:

postgresql 1.2.0


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 3:09 pm 
Newbie

Joined: Mon Apr 10, 2006 3:06 pm
Posts: 1
create afile call Person.hbm.xml save it where is your hibernate.cfg.xml


<?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="events.Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>

<set name="events" table="PERSON_EVENT">
<key column="PERSON_ID"/>
<many-to-many column="EVENT_ID" class="events.Event"/>
</set>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 8:59 pm 
Newbie

Joined: Wed Mar 22, 2006 4:46 pm
Posts: 9
I found my error. I was not getting the transaction (s.beginTransaction()) in my store method. It was strange that this was not the error generated but once I found and corrected it everything worked fine.


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.