简体   繁体   中英

@OnetoMany relationship Hibernate Spring MVC

I would like to do a simple relationship between Person and Phone. In this case, 1 person can have Many phone numbers.

Thus, I use this relationship in the models. Summarizing:

Class Person ("Pessoa"):

@Entity
public class Pessoa {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int idPessoa;

    @OneToMany(mappedBy = "pessoa", targetEntity = Telefone.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Telefone> telefone;

    public int getIdPessoa() {
        return idPessoa;
    }
    public void setIdPessoa(int idPessoa) {
        this.idPessoa = idPessoa;
    }
    public List<Telefone> getTelefone() {
        return telefone;
    }
    public void setTelefone(List<Telefone> telefone) {
        this.telefone = telefone;
    }

}

Class Phone ("Telefone"):

@Entity
public class Telefone {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int idTelefone;
    private TipoTelefone tipoTelefone;
    private int numeroTelefone;
     @ManyToOne
     @JoinColumn(name="idPessoa")
     private Pessoa pessoa;

    public TipoTelefone getTipoTelefone() {
        return tipoTelefone;
    }
    public void setTipoTelefone(TipoTelefone tipoTelefone) {
        this.tipoTelefone = tipoTelefone;
    }
    public int getNumeroTelefone() {
        return numeroTelefone;
    }
    public void setNumeroTelefone(int numeroTelefone) {
        this.numeroTelefone = numeroTelefone;
    }
}

I don't know why, but the data related to the phones are recorded, but the idPessoa (idPerson) not, then I lost the relationship between the tables.

table phone

All others datas used in the form, even OnetoOne relationship are recording well, only this OneToMay is presenting problem.

If you set such relationship and save both entities, you must set the relationship on both sides. Otherwise saving the first entity can set the relationship and saving the second entity can remove it again.

So if you set the person for a Phone to set the relationship, but you don't explicitly add the phone to the list of phones of the person. Persisting the phone sets the relation. But if person is persisted (before it was retrieved again which would fill the list), hibernate will remove the relation again because the persisted person entity doesn't have the phone in it's list.

The other way around, adding the phone to the list of phones of the person but not setting the person for the phone can do the same if the person is persisted first and the phone afterwards.

So, you have 2 options.
Option 1: If you want to persist both entities you need to manage the relation on both sides.
Option 2: Persist the entity used to set the relationship and don't persist the other one. Don't keep using it (replace it by retrieving it again from database, if you need to use it further).
If you have changes in both entities and need to save them, option 1 is the easiest choice. If you have changes in one of both entities only, you can use option 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM