[play-framework] ManyToMany : how to save / find ?

637 views
Skip to first unread message

CrYoSoRe

unread,
Apr 16, 2010, 6:23:03 AM4/16/10
to play-framework
Hy.

I'm a newbie with JPA and i dont understand how can i use the
ManyToMany relation with Play ?

Just see my code (in french i'm sorry):

/-----------------------------------------------------------------------------------/
package models;

import java.util.*;
import javax.persistence.*;

import play.db.jpa.*;

import models.FormatSiteWeb;
import models.MotCleSiteWeb;
import models.ThemeSiteWeb;

@Entity
/**
* Classe SiteWeb
* Classe utilisée pour stocker les différents site web references.
* @author a.pellet <pellet....@gmail.com>
* @see models.FormatSiteWeb;
* @since 2010/04/16
*/
public class SiteWeb extends Model {

//
--------------------------------------------------------------------
// Dependances avec les autres classes
//
--------------------------------------------------------------------
@ManyToOne
public FormatSiteWeb formatDuSiteWeb;

@ManyToMany
public List<MotCleSiteWeb> listeDeMotsClesCorrespondantsAuSiteWeb;

@ManyToOne
public ThemeSiteWeb themeDuSiteWeb;


//
--------------------------------------------------------------------
// Proprietes de la classe.
//
--------------------------------------------------------------------
public String nom;
public String slogan;
public String url;
@Lob
public String description;

/**
* Constructeur surcharge pour l'initialisation d'un site web.
* @param String format
* @param String nom
* @param String slogan
* @param String url
* @param String description
*/
public SiteWeb(FormatSiteWeb formatDuSiteWeb, ThemeSiteWeb
themeDuSiteWeb, String nom, String slogan, String url, String
description) {
//
--------------------------------------------------------------------
// Dependances avec les autres classes
//
--------------------------------------------------------------------
this.formatDuSiteWeb =
formatDuSiteWeb;
this.listeDeMotsClesCorrespondantsAuSiteWeb = new
ArrayList<MotCleSiteWeb>();
this.themeDuSiteWeb = themeDuSiteWeb;

//
--------------------------------------------------------------------
// Proprietes de la classe.
//
--------------------------------------------------------------------
this.nom = nom;
this.slogan = slogan;
this.url = url;
this.description = description;
this.nombreDePoint = nombreDePoint;

}

}

//-----------------------------------------------------------------------------------------

package models;

import java.util.*;
import javax.persistence.*;

import play.db.jpa.*;

import models.SiteWeb;

@Entity
/**
* Classe MotsClesSiteWeb
* Classe utilisée pour stocker les différents mots clés des site web.
* @author a.pellet <pellet....@gmail.com>
* @see models.SiteWeb;
* @since 2010/04/15
*/
public class MotCleSiteWeb extends Model {

//
--------------------------------------------------------------------
// Dependances avec les autres classes
//
--------------------------------------------------------------------
@ManyToMany
public List<SiteWeb> listeDeSiteWebCorrespondantsAuMotCle;

//
--------------------------------------------------------------------
// Proprietes de la classe.
//
--------------------------------------------------------------------
public String nomDuMotCle;

/**
* Constructeur surcharge pour l'initialisation d'un mot cle.
* @param String nom
*/
public MotCleSiteWeb(String nomDuMotCle) {
//
--------------------------------------------------------------------
// Dependances avec les autres classes
//
--------------------------------------------------------------------
this.listeDeSiteWebCorrespondantsAuMotCle = new
ArrayList<SiteWeb>();

//
--------------------------------------------------------------------
// Proprietes de la classe.
//
--------------------------------------------------------------------
this.nomDuMotCle = nomDuMotCle;
}

}

//----------------------------------------------------------------------------------------------

I have no probleme to save / find with ManyToOne or OneToMany but i
dont know how can i save / find on ManyToMany relation.

Someone have an exemple ?

Thanks.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Guillaume Bort

unread,
Apr 16, 2010, 2:27:23 PM4/16/10
to play-fr...@googlegroups.com
What is the problem exactly? Can you be more precise?

Dam74

unread,
Apr 17, 2010, 5:07:28 AM4/17/10
to play-framework
When you map a same "ManyToMany relationShip" in both directions, you
need to tell which of these two directions is (kind of) the "master".
So : one of your @ManyToMany annotations must have the info :
"mappedBy" :

For example : if you have two entities : "Event" and "User", and a
User can "follow" several "Events",
and an Event can be followed by several Users ("ManyToMany
relationShip").

You would have :

in the entity User :
@ManyToMany(cascade=CascadeType.ALL)
public Set<Event> followedEvents = new HashSet<Event>();

in the entity Event :
@ManyToMany(mappedBy="followedEvents")
public Set<User> followsByUsers = new HashSet<User>();

A+ !

Dam74


On Apr 16, 12:23 pm, CrYoSoRe <pellet.aurel...@gmail.com> wrote:
> Hy.
>
> I'm a newbie with JPA and i dont understand how can i use the
> ManyToMany relation with Play  ?
>
> Just see my code (in french i'm sorry):
>
> /-----------------------------------------------------------------------------------/
> package models;
>
> import java.util.*;
> import javax.persistence.*;
>
> import play.db.jpa.*;
>
> import models.FormatSiteWeb;
> import models.MotCleSiteWeb;
> import models.ThemeSiteWeb;
>
> @Entity
> /**
> * Classe SiteWeb
> * Classe utilisée pour stocker les différents site web references.
> * @author a.pellet <pellet.aurel...@gmail.com>
> * @author a.pellet <pellet.aurel...@gmail.com>

CrYoSoRe

unread,
Apr 21, 2010, 5:46:08 AM4/21/10
to play-framework
@Dam74

Thanks for your help !

@Guillaume Bort

I just have some difficulty to understand the ManyToMany relation :)
(And it's not easy to explain a problem when you dont understand the
subject)

Maybe because in a OneToMany or ManyToOne there is an explicit
"master" - "slave" dependency ?
Reply all
Reply to author
Forward
0 new messages