4 pièce(s) jointe(s)
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:
Pièce jointe 453285
la colonne tenant_id est vide :
Pièce jointe 453286
voici mes deux tables
Pièce jointe 453200
Pièce jointe 453209
and my code :
Class Tenant
Code:
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:
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:
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:
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:
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);
} |