-->
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.  [ 1 post ] 
Author Message
 Post subject: Question about relation many to many and session
PostPosted: Sun Feb 03, 2013 12:45 pm 
Newbie

Joined: Sun Feb 03, 2013 11:54 am
Posts: 1
Hello,

I m beginner in Hibernate and i m performing some tests on database insertion, entity relations ...

Here is a behaviour that i couldn't find an explanation for it, :

I have two entity : Event and Guest and a many to many relationship beween them.

The Event class has a guestList attribute containing all Guests linked to the event :

----------------------------------------------------------------------------------------------
[color=#BF0000][b][color=#FF0000]@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "events",
targetEntity = Guest.class
)
private List<Guest> guestList;
[/b]
[/color][/color]
----------------------------------------------------------------------------------------------
The Guest class has a events attribute containing all events linked to the event :

----------------------------------------------------------------------------------------------
[color=#BF0000]@ManyToMany(
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
joinColumns=@JoinColumn(name="guestId"),
inverseJoinColumns=@JoinColumn(name="eventId")
)
private List<Event> events;
[/color]
----------------------------------------------------------------------------------------------
Now i have a Main class which contains a testCreate() method that creates an event and a guest, add the event to the event list of the guest but doesn't add the guest to the guest list of the event. The Main class also contains a printEvents() method that displays all the events and its related guests.

I implemented the main() methos it in two different way, one method display the related guests but the second one doesn't diplay related guests although the guest and the event are mapped in the guest_events mapping table in the database :/ :


[color=#FF0000]Method 1 : Ok, diplays events and related guest[/color]

----------------------------------------------------------------------------------------------
public static void main(String[] args) {
// open session Hibernate
s = HibernateUtils.getSession();

// Create a guest and an event and add the event in the event list of the guest
testCreate();


// Close session Hibernate
s.close();
// Reopen session Hibernate
s = HibernateUtils.getSession();
// displays events and its related guests
printEvents();
// Close session Hibernate
s.close();
}

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

[color=#FF0000]Method 2 :Ko, diplays events but doesn(t diplay related guests, although the guest and the event are mapped in the guest_events mapping table in the database[/color]

----------------------------------------------------------------------------------------------
public static void main(String[] args) {
// open session Hibernate
s = HibernateUtils.getSession();

// Create a guest and an event and add the event in the event list of the guest
testCreate();
// displays events and its related guests
printEvents();

// Close session Hibernate
s.close();

}


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

Here is the full code of the Main class :

public class Main {
private static Session s = null;

public static void main(String[] args) {
// open session Hibernate
s = HibernateUtils.getSession();

// Create a guest and an event
testCreate();


// Close session Hibernate
s.close();
// Reopen session Hibernate
s = HibernateUtils.getSession();
printEvents();
// Close session Hibernate
s.close();
}

// Test de la persistance d'objets comportant une association
private static void testCreate() {
//Création des objets à rendre persistants


Transaction tx = s.beginTransaction();
Event e = new Event("evenemet e", "description", true);

Guest g = new Guest("Invité 1", "invite@mistra.fr");


ArrayList<Event> eventList = new ArrayList<Event>();
eventList.add(e);

//Le Guest g est invité aux deux Events
g.setEvents(eventList);


// Enregistrements
s.persist(g);

tx.commit();

}

// Affiche le contenu de la table EVENTS
@SuppressWarnings({ "unchecked", "deprecation" })


private static void printEvents() {
System.out.println("---Events---");
// Création de la requête
Query q = s.createQuery("from Event");
ArrayList<Event> list = (ArrayList<Event>) q.list();
// Affichage de la liste de résultats
for (Event e: list) {
System.out.println("[id] = " + e.getId() + "\t" +

"[title] = " + e.getTitle() + "\t" +

"[allDay] = " + e.isAllDay());


System.out.print("[guests ID ]: ");
if (e.getGuestList() != null) {
for (Guest g : e.getGuestList())
System.out.print(g.getId() + " ");
}
System.out.println();
}
}


}




Can some one give an explanation for thios behaviour ?
Thank you.

Regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.