IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Boot Java Discussion :

probleme of Relation ManyToOne in springboot application


Sujet :

Spring Boot Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 75
    Points : 50
    Points
    50
    Par défaut probleme of Relation ManyToOne in springboot application
    bonjour les développeurs

    j'ai un petit soucis, et j’espère que vous allez m'aider a le résoudre..
    je crée un simple CRUD avec 2 classes Tenant et Adresses : chaque tenant possède plusieurs adresses.
    le problème : quand je crée un tenant, il enregistre pas le tenant_id dans la table adresses, pour que je puisse savoir l'adresse de qui..
    voici le test dans PostMan:

    Nom : postman.PNG
Affichages : 866
Taille : 176,2 Ko

    la colonne tenant_id est vide :

    Nom : bdd.PNG
Affichages : 776
Taille : 141,7 Ko
    voici mes deux tables
    Nom : ten.PNG
Affichages : 766
Taille : 89,5 Ko

    Nom : add.PNG
Affichages : 731
Taille : 102,8 Ko


    and my code :

    Class Tenant

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package tech.Alphalupi.Tenant_Api.Entities;
     
    import lombok.Data;
    import java.util.List;
     
    import javax.persistence.*;
     
     
    @Data
    @Entity
     
    public class Tenant {
     
        @Id
        @GeneratedValue (strategy=GenerationType.AUTO)
        private Long id;
     
     
        private String name;
     
        private String email; 
     
        private Long telephone;
     
        @OneToMany(mappedBy = "tenant" , cascade = CascadeType.ALL)
        private List<Address> address;
     
     
     
     
     
    }

    class Address

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    package tech.Alphalupi.Tenant_Api.Entities;
     
     
     
    import lombok.Data;
    import javax.persistence.*;
     
    @Data
    @Entity
    public class Address {
     
    	@Id
        @GeneratedValue(strategy = GenerationType.AUTO)
    	  private Long id;
     
    	  private String city;
     
    	  private String stateOrProvince;
     
    	  private String country;
     
    	  private String postalCode;
     
    	  @ManyToOne
    	  private Tenant tenant;
     
    }

    mon controlleur :

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    package tech.Alphalupi.Tenant_Api.Controller;
     
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
     
    import tech.Alphalupi.Tenant_Api.Entities.Tenant;
    import tech.Alphalupi.Tenant_Api.Service.TenantService;
     
    import java.net.URISyntaxException;
    import java.util.Optional;
     
    @RestController
    @RequestMapping("/api/")
    @CrossOrigin
    class TenantController {
     
    	@Autowired
    	private TenantService tenantService;
     
       @GetMapping("/tenants")
         public Iterable<Tenant> getAllTenants() {
         return tenantService.FindAllTenants();
         }
     
       @GetMapping("/tenant/{id}")
       Optional<Tenant> getTenantById(@PathVariable Long id) {
    	   return tenantService.findTenantById(id); 
      }
     
        @PostMapping("/tenant")   	
            public ResponseEntity<?> createTenant(@RequestBody Tenant tenant) throws URISyntaxException {
        	Tenant newTenant = tenantService.createTenant(tenant);	
        	return new ResponseEntity<Tenant>(newTenant, HttpStatus.CREATED);
      }
     
    	@PutMapping("/tenant")
    	public ResponseEntity<?> updateTenant(@RequestBody Tenant tenant) throws URISyntaxException {
        	Tenant newTenant = tenantService.createTenant(tenant);	
        	return new ResponseEntity<Tenant>(newTenant, HttpStatus.CREATED);
        	}
     
     @DeleteMapping("/tenant/{id}")
       public void deleteTenant(@PathVariable Long id) {
    	 tenantService.deleteTenant(id);
     
      }
    }

    mon service

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    package tech.Alphalupi.Tenant_Api.Service;
     
     
    import java.util.Optional;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.bind.annotation.PathVariable;
     
    import tech.Alphalupi.Tenant_Api.Entities.Tenant;
    import tech.Alphalupi.Tenant_Api.Repository.TenantRepo;
     
     
     
    @Service
    public class TenantService {
     
    	@Autowired
    	private TenantRepo tenantRepo;
     
    	public Tenant createTenant(Tenant tenant) {
    	        return tenantRepo.save(tenant) ;   }
     
     
    	public Iterable<Tenant> FindAllTenants() {
         return tenantRepo.findAll(); }
     
     
    	public Optional<Tenant> findTenantById(Long id) {
    		return tenantRepo.findById(id);	}
     
     
    	public Tenant updateTenant(Long id) {
    		tenantRepo.findById(id);
    		return tenantRepo.save(updateTenant(id));
     
    	}
     
    	 public void deleteTenant(@PathVariable Long id) {
    		  tenantRepo.deleteById(id);  }
     
     
    }

    Et enfin le repository :

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package tech.Alphalupi.Tenant_Api.Repository;
     
    import java.util.List;
     
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.repository.query.Param;
    import tech.Alphalupi.Tenant_Api.Entities.Tenant;
     
     
    public interface TenantRepo extends JpaRepository<Tenant, Long> {
     
    	List<Tenant> findByName(@Param("name") String name);
     
    }

  2. #2
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    Salut
    comment est ce que tu crees l´objet Tenant?

    Eric

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 75
    Points : 50
    Points
    50
    Par défaut
    j ai pas bien compris ta question..
    j ai l'entity tenant qui cree une table dans la BDD

  4. #4
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 75
    Points : 50
    Points
    50
    Par défaut
    tic tac...

  5. #5
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    Salut,
    l´objet que tu veux sauvegarder dans la base de données , comment est ce que tu l´a crée?

    Eric

  6. #6
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 75
    Points : 50
    Points
    50
    Par défaut
    je le fais dans postman

    j'envoi un post request avec l objet que tenant et dedans l objet address

    Nom : possssss.PNG
Affichages : 705
Taille : 175,0 Ko

  7. #7
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 75
    Points : 50
    Points
    50
    Par défaut
    je me suis tromper de forum peu etre??
    je me rappelle bien qu'il étais actif et les gens s’entraidait bien ici ... c mort ou quoi ??

  8. #8
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut

    changes tes classes comme ceci :
    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    	import lombok.Data;
    import java.util.List;
    
    import javax.persistence.*;
    
    
    @Data
    @Entity
    
    public class Tenant {
    	
        @Id
        @GeneratedValue (strategy=GenerationType.AUTO)
        private Long id;
        
       
        private String name;
        
        private String email; 
        
        private Long telephone;
        
        @OneToMany(mappedBy = "tenant" , cascade = CascadeType.ALL)
    	@JoinColumn(referencedColumnName="ID", name="TENANT_ID")
        private List<Address> address;
     
    }

    et

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import lombok.Data;
    import javax.persistence.*;
    
    @Data
    @Entity
    public class Address {
    	
    	@Id
        @GeneratedValue(strategy = GenerationType.AUTO)
    	  private Long id;
    	  
    	  private String city;
    	  
    	  private String stateOrProvince;
    	  
    	  private String country;
    	  
    	  private String postalCode;
    	  
    	  @ManyToOne
    	  @JoinColumn(name = "TENANT_ID")
    	  private Tenant tenant;
    
    }

    eric

  9. #9
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 75
    Points : 50
    Points
    50
    Par défaut
    problème résolu !
    la solution : j'ai ajouté les deux ligne suivante

    Nom : 1.PNG
Affichages : 726
Taille : 31,3 Ko

    maintenant j'ai le résultat attendu

    Nom : 2.PNG
Affichages : 708
Taille : 21,3 Ko

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [3.x] Probleme relation ManyToOne
    Par Hollux dans le forum Symfony
    Réponses: 1
    Dernier message: 17/05/2016, 17h34
  2. Probleme avec une relation ManytoOne
    Par decad7 dans le forum Doctrine2
    Réponses: 1
    Dernier message: 04/02/2013, 18h21
  3. Probleme de relations dans une base access.
    Par fredhali2000 dans le forum Access
    Réponses: 6
    Dernier message: 21/02/2006, 16h36
  4. access2000, probleme creation relations de tables
    Par nono_60 dans le forum Access
    Réponses: 2
    Dernier message: 12/09/2005, 22h50
  5. Probleme de relation
    Par pod1978 dans le forum Décisions SGBD
    Réponses: 1
    Dernier message: 30/03/2005, 15h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo