framework-web:spring:models

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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:02] – [Classes] jcheronframework-web:spring:models [2026/10/12 08:09] (Version actuelle) – [Création du projet] jcheron
Ligne 6: Ligne 6:
 Créer un projet en ajoutant les dépendances suivantes : Créer un projet en ajoutant les dépendances suivantes :
  
 +<html><div class="imageB"></html>
 {{:framework-web:spring:dep-h2.png?|}} {{:framework-web:spring:dep-h2.png?|}}
 +<html></div></html>
 +
 +==== Pom.xml ====
 +
 +
 +<sxh xml>
 +        <dependency>
 +            <groupId>org.springframework.boot</groupId>
 +            <artifactId>spring-boot-starter-data-jpa</artifactId>
 +        </dependency>
 +        
 +        <dependency>
 +     <groupId>org.springframework.boot</groupId>
 +     <artifactId>spring-boot-starter-data-jpa-test</artifactId>
 +     <scope>test</scope>
 + </dependency>
 +        <dependency>
 +            <groupId>com.h2database</groupId>
 +            <artifactId>h2</artifactId>
 +            <scope>runtime</scope>
 +        </dependency>
 +</sxh>
 ==== Configuration ==== ==== Configuration ====
  
Ligne 63: Ligne 86:
  
 <sxh java> <sxh java>
-public interface OrgaRepository extends JpaRepository<Organization,Integer> { +public interface OrgaRepository extends JpaRepository<Organization, Integer> { 
 +    List<Organization> findByDomain(String domain); 
 +    List<Organization> findByName(String name);
 } }
 +
 </sxh> </sxh>
 +
 + [[https://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html|JPA Repositories]]
  
 === Test d'ajout d'une instance === === Test d'ajout d'une instance ===
Ligne 82: Ligne 109:
  @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 92: Ligne 119:
 === ManyToOne === === ManyToOne ===
  
 +Chaque groupe appartient à une organisation.
  
 +<sxh java>
 +@Entity
 +public class Groupe {
 + @Id
 + @GeneratedValue(strategy=GenerationType.AUTO)
 + private int id;
 +
 + @ManyToOne
 + private Organization organization;
 +}
 +</sxh>
  
 +
 +=== 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,mappedBy="organization")
 + private List<Groupe> groupes;
 +}
 +</sxh>
 +
 +=== 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="users")
 + private List<Groupe> groupes;
 +}
 +</sxh>
 +
 +<sxh java>
 +@Entity
 +public class Groupe{
 + @Id
 + @GeneratedValue(strategy=GenerationType.AUTO)
 + private int id;
 +
 + @ManyToOne
 + private Organization organization;
 +
 + @ManyToMany
 + @JoinTable(name = "user_group")
 + private List<User> users;
 +}
 +</sxh>
 +
 +==== Logs ====
 +Pour tracer les logs Hibernate et visualiser les requêtes, activez les options suivantes dans application.properties :
 +
 +<sxh>
 +#spring.jpa.show-sql=true
 +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
 +</sxh>
  
  • framework-web/spring/models.1549573341.txt.gz
  • Dernière modification : il y a 7 ans
  • (modification externe)