Visualizzazione dei risultati da 1 a 10 su 14

Visualizzazione discussione

  1. #1

    Aiuto su relazioni in Spring Boot

    ciao!

    sto cercando visualizzare i dati con join tra tabelle.
    ho una tabella utenti:
    • id
    • email
    • password


    una tabella attivita:
    • a_id
    • a_utente
    • a_descrizione


    ogni utente può avere N attività.
    ogni attività può essere solo di un utente.

    queste le due classi:
    codice:
    import javax.persistence.*;
    import java.util.List;
    
    @Entity
    @Table(name = "utenti")
    public class Utente {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;
    
        private String email;
        private String password;
    
        @OneToMany(mappedBy = "utente")
        private List<Attivita> attivita;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    e attivita:
    codice:
    import javax.persistence.*;
    
    @Entity
    @Table(name = "attivita")
    public class Attivita {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;
    
        @ManyToOne
        @JoinColumn(name = "a_utente")
        private Utente utente;
    
        @Column(name = "a_attivita")
        private String descrizione;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Utente getUtente() {
            return utente;
        }
    
        public void setUtente(Utente utente) {
            this.utente = utente;
        }
    
        public String getDescrizione() {
            return descrizione;
        }
    
        public void setDescrizione(String descrizione) {
            this.descrizione = descrizione;
        }
    }
    a questo punto vorrei aggiungere le attività nella risposta json del controller utente.
    ma qui mi perdo:
    codice:
    import com.mp.springtest.model.Utente;
    import com.mp.springtest.model.UtentiRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    import java.util.Optional;
    
    @RestController
    @RequestMapping(path = "/utenti")
    public class UtentiController {
    
        @Autowired
        private UtentiRepository utentiRepository;
    
        @GetMapping(path = "/all")
        public @ResponseBody
        List<Utente> getAll() {
            return utentiRepository.findAll();
        }
    
        @GetMapping(path = "/utente/{utenteId}")
        public @ResponseBody
        Optional<Utente> getById(@PathVariable Integer utenteId) {
            return utentiRepository.findById(utenteId);
        }
    
    }
    Ultima modifica di LeleFT; 10-12-2021 a 14:40

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.