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/07 22:01] – [Classes] jcheron | framework-web:spring:models [2025/08/12 02:35] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 63: | Ligne 63: | ||
| <sxh java> | <sxh java> | ||
| - | public interface OrgaRepository extends JpaRepository< | + | public interface OrgaRepository extends JpaRepository< |
| + | | ||
| + | List< | ||
| } | } | ||
| + | |||
| </ | </ | ||
| + | |||
| + | | ||
| === Test d' | === Test d' | ||
| Ligne 82: | Ligne 86: | ||
| @ResponseBody | @ResponseBody | ||
| public String newOrga(Organization orga) { | public String newOrga(Organization orga) { | ||
| - | repo.save(orga); | + | repo.saveAndFlush(orga); |
| return orga+" ajoutée."; | return orga+" ajoutée."; | ||
| } | } | ||
| Ligne 88: | Ligne 92: | ||
| </ | </ | ||
| + | ==== 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< | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Logs ==== | ||
| + | Pour tracer les logs Hibernate et visualiser les requêtes, activez les options suivantes dans application.properties : | ||
| + | |||
| + | <sxh> | ||
| + | # | ||
| + | spring.jpa.properties.hibernate.format_sql=true | ||
| + | # logs the SQL statements | ||
| + | # basic log level for all messages | ||
| + | logging.level.org.hibernate=info | ||
| + | # SQL statements and parameters | ||
| + | logging.level.org.hibernate.SQL=debug | ||
| + | logging.level.org.hibernate.orm.jdbc.bind=trace | ||
| + | </ | ||