Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
framework-web:spring:models [2019/02/04 02:52] – [Configuration] jcheron | framework-web:spring:models [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 33: | Ligne 33: | ||
{{: | {{: | ||
+ | === Création d'une Entité === | ||
+ | <sxh java> | ||
+ | package s4.spring.td2.models; | ||
+ | |||
+ | import javax.persistence.Entity; | ||
+ | import javax.persistence.GeneratedValue; | ||
+ | import javax.persistence.GenerationType; | ||
+ | import javax.persistence.Id; | ||
+ | |||
+ | @Entity | ||
+ | public class Organization { | ||
+ | @Id | ||
+ | @GeneratedValue(strategy=GenerationType.AUTO) | ||
+ | private int id; | ||
+ | |||
+ | private String name; | ||
+ | private String domain; | ||
+ | private String aliases; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | __Annotations :__ | ||
+ | * @Entity | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | === Création d'un Repository === | ||
+ | |||
+ | <sxh java> | ||
+ | public interface OrgaRepository extends JpaRepository< | ||
+ | List< | ||
+ | List< | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | | ||
+ | |||
+ | === Test d' | ||
+ | Les données sont postées depuis un formulaire contenant les champs name, domain et aliases : | ||
+ | |||
+ | <sxh java> | ||
+ | @Controller | ||
+ | @RequestMapping("/ | ||
+ | public class OrgasController { | ||
+ | |||
+ | @Autowired | ||
+ | private OrgaRepository repo; | ||
+ | |||
+ | @PostMapping(" | ||
+ | @ResponseBody | ||
+ | public String newOrga(Organization orga) { | ||
+ | repo.saveAndFlush(orga); | ||
+ | return orga+" ajoutée."; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Relations ==== | ||
+ | |||
+ | === ManyToOne === | ||
+ | |||
+ | Chaque groupe appartient à une organisation. | ||
+ | |||
+ | <sxh java> | ||
+ | @Entity | ||
+ | public class Groupe { | ||
+ | @Id | ||
+ | @GeneratedValue(strategy=GenerationType.AUTO) | ||
+ | private int id; | ||
+ | |||
+ | @ManyToOne | ||
+ | private Organization organization; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | === OneToMany === | ||
+ | |||
+ | Chaque organisation possède plusieurs groupes. (la relation est bi-directionnelle dans ce cas) | ||
+ | |||
+ | <sxh java> | ||
+ | @Entity | ||
+ | public class Organization { | ||
+ | @Id | ||
+ | @GeneratedValue(strategy=GenerationType.AUTO) | ||
+ | private int id; | ||
+ | |||
+ | private String name; | ||
+ | private String domain; | ||
+ | private String aliases; | ||
+ | |||
+ | @OneToMany(cascade=CascadeType.ALL, | ||
+ | private List< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === ManyToMany === | ||
+ | |||
+ | Chaque utilisateur peut appartenir à plusieurs groupes. Dans chaque groupe, on a plusieurs utilisateurs. | ||
+ | |||
+ | <sxh java> | ||
+ | @Entity | ||
+ | public class User { | ||
+ | @Id | ||
+ | @GeneratedValue(strategy=GenerationType.AUTO) | ||
+ | private int id; | ||
+ | |||
+ | @ManyToOne | ||
+ | private Organization organization; | ||
+ | |||
+ | @ManyToMany(mappedBy=" | ||
+ | private List< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <sxh java> | ||
+ | @Entity | ||
+ | public class Groupe{ | ||
+ | @Id | ||
+ | @GeneratedValue(strategy=GenerationType.AUTO) | ||
+ | private int id; | ||
+ | |||
+ | @ManyToOne | ||
+ | private Organization organization; | ||
+ | |||
+ | @ManyToMany | ||
+ | @JoinTable(name = " | ||
+ | private List< | ||
+ | } | ||
+ | </ |