views:

248

answers:

3

Hello good fellas! i 'm trying the hibernate tutorials from their main site and wanted to change thing a bit to know how many to many relationship work with java.util.set interface.My mappings are correct and i can insert in and from the tables EVENT, PERSON and the mapping table PERSON_EVENT.Now i've inserted some dummy values in the tables and add their mappings in the mapping table.I wanted to display all the events of all the person who are register to an event or more. with this code :

 public void ShowPersonEvents()
 {
     Person aperson;
     Event anEvent;
     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
     session.beginTransaction();
     List<Person> persons = session.createQuery("from Person").list();
     for(int i =0; i< persons.size(); i++)
     {
         aperson = (Person) persons.get(i);
         Set a = aperson.getEvents();
//            String[] events = (String[])a.toArray(new String[a.size()]);
//             for (String e : events)
//             {
//                 System.out.println(aperson.getLastname()+" is registerd to the" + e);
//
//             }
         Iterator it = a.iterator();
         while(it.hasNext())
         {
             System.out.println(aperson.getLastname()+" is registerd to the" +(String) it.next().toString());
         }

//                System.out.println();
         }
         session.getTransaction().commit();
     }
}

so when i run is the who the correct number of rows but instead of show for example rows like :

Joseph is registered to the opensouce event

it's rather showing something like :

Joseph is registered to the domain.Event@18a8ce2

this is the format mypackagename.myclassname@something. when i comment the iterator part ant uncomment the casting to an string array i have an exception:arraystoreexception. I'm kind of lost a bit.I can't see what is wrong here.Please can you have a look and tell me what i did wrong?Thanks for reading.

+3  A: 

This doesn't really have anything to do with Hibernate. You're calling toString() on the Event object:

(String) it.next().toString()

You haven't overridden the Event.toString() method, so you're getting the default implementation. Instead try something like:

while(it.hasNext()) {
    Event event = (Event) it.next();
    System.out.println(aperson.getLastname()+" is registerd to the" + event.getName());
}

You can also improve your Hibernate HQL query by pre-fetching the events. As it stands now they will be lazy loaded so you'll get an extra query for each person (assuming you haven't set the fetching strategy in the mapping file).

Try something like:

List<Person> persons = session.createQuery("from Person p left join fetch p.events").list();
geofflane
thanks man.Your approach works fine with me.but I want to override that toString function is it as simple as returning something like retun this.getTitle(); or any combination of it?
black sensei
Yes, it's that easy:public String toString() { return this.title;}
geofflane
+1  A: 

The reason you are seeing domain.Event@18a8ce2 is that this is the output from calling Object.toString() (i.e. the default toString() implementation). This implementation returns a String in the format @. If you wish to see the event's internal state you should override the toString() method in your Event class definition:

public String toString() {
  return String.format("Event{ID: %d, Title: %s, Date: %s}", id, title, date);
}

The reason for the ArrayStoreException is that you're trying to create a String[] but are passing in objects that are not Strings (they're Events). From the ArrayStoreException Javadoc:

"Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects."

So you need to create your Array by calling toArray(new Event[a.size()]).

Adamski
very satisfied with the explanation.thanks
black sensei
i totally ignored that what it returns are event objects.my bad
black sensei
+1  A: 

You haven't shown us your implementation of Person and Event class so I can only guess. To me it looks like you haven't overridden toString method in Event class, as the output looks like the result of the toString method inherited from Object

Tadeusz Kopec