Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Ejb 3

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    17

    Ejb 3

    Problema,

    Da premettere che sto usando gli EJB3


    @EJB
    private UtenteFacadeLocal utenteFacade;


    @EJB
    private StoriaUtenteFacadeLocal storiaUtenteFacade;




    Utente utente = new Utente();
    StoriaUtente storiaUtente = new StoriaUtente();



    Ho 2 Tabelle... una tabella utente e una tabella storiaUtente...

    Mi creo l'utente e quindi mi prendo da un form HTML nome e cognome dell'utente:



    String nome = request.getParameter(“nome”);
    String cognome = request.getParameter(“cognome”);


    utente.setNome(nome);
    utente.setCognome(cognome);


    e mi prendo anche i campi per la storia dell'utente


    String inizioStoria = request.getParameter(“inizioStoria”);


    storiaUtente.setInizioStoria(inizioStoria);


    Adesso devo fare in modo che quando creo l'utente, automaticamente aggiungo anche la storia nella tabella StoriaUtente, insomma faccio una Foreign Key, quindi associo idUtente a idStoriaUtente


    Adesso per far funzionare la foreign key come devo proseguire?Non basta associare in mysql le due chiavi primarie, quindi come proseguo?


    Ovviamente la creazione dell'utente la faccio chiamato il metodo create:


    utenteFacade.create(utente);


    Facendo così però aggiungo soltanto nome e cognome alla tabella utenti, mentre alla tabella storiaUtenti non succede nulla.


    Spero che abbiate capito il mio problema...

  2. #2
    Utente di HTML.it L'avatar di bako
    Registrato dal
    Feb 2004
    Messaggi
    1,797
    edit: posta il codice degli Entity bean

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    17

    ^^


    @Entity
    @Table(name = "utente")
    @NamedQueries({@NamedQuery(name = "Utente.findAll", query = "SELECT u FROM Utente u"), @NamedQuery(name = "Utente.findByIdutente", query = "SELECT u FROM Utente u WHERE u.idutente = :idutente"), @NamedQuery(name = "Utente.findByNome", query = "SELECT u FROM Utente u WHERE u.nome = :nome"), @NamedQuery(name = "Utente.findByCognome", query = "SELECT u FROM Utente u WHERE u.cognome = :cognome")})
    public class Utente implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idutente")
    private Integer idutente;
    @Basic(optional = false)
    @Column(name = "nome")
    private String nome;
    @Basic(optional = false)
    @Column(name = "cognome")
    private String cognome;
    @JoinColumn(name = "idutente", referencedColumnName = "idstoria", insertable = false, updatable = false)
    @OneToOne(optional = false)
    private Storiautente storiautente;

    public Utente() {
    }

    public Utente(Integer idutente) {
    this.idutente = idutente;
    }

    public Utente(Integer idutente, String nome, String cognome) {
    this.idutente = idutente;
    this.nome = nome;
    this.cognome = cognome;
    }

    public Integer getIdutente() {
    return idutente;
    }

    public void setIdutente(Integer idutente) {
    this.idutente = idutente;
    }

    public String getNome() {
    return nome;
    }

    public void setNome(String nome) {
    this.nome = nome;
    }

    public String getCognome() {
    return cognome;
    }

    public void setCognome(String cognome) {
    this.cognome = cognome;
    }

    public Storiautente getStoriautente() {
    return storiautente;
    }

    public void setStoriautente(Storiautente storiautente) {
    this.storiautente = storiautente;
    }

    @Override
    public int hashCode() {
    int hash = 0;
    hash += (idutente != null ? idutente.hashCode() : 0);
    return hash;
    }

    @Override
    public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Utente)) {
    return false;
    }
    Utente other = (Utente) object;
    if ((this.idutente == null && other.idutente != null) || (this.idutente != null && !this.idutente.equals(other.idutente))) {
    return false;
    }
    return true;
    }

    @Override
    public String toString() {
    return "entities.ute.Utente[idutente=" + idutente + "]";
    }

    }

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2009
    Messaggi
    17

    ...

    quello di storia utente è inutile ovviamente... è in utente che ho la foreing key

  5. #5

    EJB 3

    Ciao,

    In teoria dovresti postare anche l'entity bean dell'altra classe.

    Comunque fare una foreign tra due tabelle con ejb3, è la cosa più semplice che ci possa essere, in quanto non hai bisogno di scrivere query e di conseguenza non hai bisogno di far la inner join col codice SQL.

    Innanzitutto devi assicurarti che i due entity bean abbiano queste righe di codice:



    @JoinColumn(name = "idtabellaprincipale", referencedColumnName = "idtabellasecondaria", insertable = false, updatable = false)
    @ManyToOne
    @OneToOne(optional = false)




    @OneToOne(cascade = CascadeType.ALL, mappedBy = "idtabellasecondaria")

    Consiglio, se usi netbeans fatti creare tutto da lui...se non sai cosa fare ti invio il video che ho creato...


    Fatto ciò basta che ti setti i campi come fai normalmente, cioè li recuperi con il request.getParameter(); e poi li setti con gli oggetti delle due classi per esempio:



    TabellaPrincipale oggetto = new TabellaPrincipale();
    TabellaSecondaria oggettoDue = new TabellaSecondaria();

    oggetto.SetNomeCampo(NomeRecuperato);

    Infine:

    oggetto.setTabellaSecondaria(oggettoSecondario);

    ti chiami il metodo nativo create, ed il gioco è fatto.

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.