-->
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: Problems with retrieving a SET
PostPosted: Wed Apr 04, 2007 3:49 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Hi,

I expanded the mapping tutorial (http://www.hibernate.org/hib_docs/v3/re ... sociations)and run in some problems. Perhaps you can help me.

I have three database tables like described in the tutorial:
- person
- event
- person_event

I want to list all events of a person.

Here is my mapping file Person.hbm.xml:
Code:
<?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" lazy="false"/>
       </set>
    </class>

</hibernate-mapping>



This is the method where I want to list all events of all Persons. The problem is that I get an error when I want to get a Set of events belonging to the person.
Perhaps you can help me (I am a hibernate newbie :) )

Here is my method code:
Code:
private List listPersons() {
       Session session = HibernateUtil.getSessionFactory().getCurrentSession();
       
       session.beginTransaction();
       
       List result = session.createQuery("from Person").list();
       
       
       
       for (int i = 0; i < result.size(); i++) {
           Person aPerson = (Person) result.get(i);
           System.out.println("Person: " + aPerson.getFirstname() + " " + aPerson.getLastname() + " participates in the following events:");
           HashSet pSet =  (HashSet) aPerson.getEvents();
           
              System.out.println("Event count: " + aPerson.getEvents().toArray().length);
          
        }
       session.getTransaction().commit();
       
       return result;
    }


Furthermore here are the two classes of event and person:

Class person:
Code:
public class Person {

    private Long id;
    private int age;
    private String firstname;
    private String lastname;
    private Set events = new HashSet();

    //getter and setter methods


Class Event:
Code:
    private Long id;
    private String title;
    private Date date;

    //getter and setter methods

Regards,
TMK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 6:44 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Which error do you have ?

I think it is a ClassCastException. Here is the problem :
Code:
HashSet pSet =  (HashSet) aPerson.getEvents();

HahSet is an implementation, Set is the interface an you have to program by interface. Internally hibernate does not use HashSet but it's own implementation PersistentSet which deal with persistent features like lazy loading.
Here is the code you must have :
Code:
Set pSet =  aPerson.getEvents();

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 10:55 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi tmk,

what is exactly your problem. You forgot to post your stack trace

chucky


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.