Foreign key is not printed in table

Asked

Viewed 46 times

3

I have an entity that has the Onetoone relationship with another, but the foreign key of the other entity is not being printed in the table, the field is empty

Entity having the Onetoone relationship

@Entity
public class LivroOrdem implements Serializable {

private static final long serialVersionUID = 1L;
// ATRIBUTOS
private Long idLivroOrdem;
private Art art;
private Date dataRealInicio;
private Date dataPrevistaConclusaoObra;
//private List<Relato> relatos;
private Relato relato;

// @OneToMany(fetch = FetchType.LAZY, mappedBy = "livroOrdem")
// CONSTRUTOR PADRÃO
public LivroOrdem() {
}

// OUTROS CONSTRUTORES
public LivroOrdem(Date dataRealInicioObra, Date dataPrevistaConclusaoObra) {
    this.dataRealInicio = dataRealInicioObra;
    this.dataPrevistaConclusaoObra = dataPrevistaConclusaoObra;
}

// GETTERS E SETTERS
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
//@Column(name = "idlivroOrdem", unique = true, nullable = false)
public Long getIdLivroOrdem() {
    return idLivroOrdem;
}

public void setIdLivroOrdem(Long idLivroOrdem) {
    this.idLivroOrdem = idLivroOrdem;
}

@Temporal(javax.persistence.TemporalType.TIMESTAMP)
public Date getDataRealInicio() {
    return dataRealInicio;
}

public void setDataRealInicio(Date dataRealInicio) {
    this.dataRealInicio = dataRealInicio;
}

@OneToOne
public Art getArt() {
    return art;
}

public void setArt(Art art) {
    this.art = art;
}

@Temporal(javax.persistence.TemporalType.TIMESTAMP)
public Date getDataPrevistaConclusaoObra() {
    return dataPrevistaConclusaoObra;
}

public void setDataPrevistaConclusaoObra(Date dataPrevistaConclusaoObra) {
    this.dataPrevistaConclusaoObra = dataPrevistaConclusaoObra;
}

/*
@OneToMany(mappedBy = "relatos")
public List<Relato> getRelatos() {
return relatos;
}
public void setRelatos(List<Relato> relatos) {
this.relatos = relatos;
}*/

@ManyToOne
public Relato getRelato() {
    return relato;
}

public void setRelato(Relato relato) {
    this.relato = relato;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 67 * hash + (this.idLivroOrdem != null ? this.idLivroOrdem.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final LivroOrdem other = (LivroOrdem) obj;
    if (this.idLivroOrdem != other.idLivroOrdem && (this.idLivroOrdem == null || !this.idLivroOrdem.equals(other.idLivroOrdem))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "LivroOrdem{" + "idLivroOrdem=" + idLivroOrdem 
            + ", art=" + art 
            + ", dataRealInicioObra=" + dataRealInicio 
            + ", dataPrevistaConclusaoObra=" + dataPrevistaConclusaoObra 
            + ", relato=" + relato + '}';
}

}

Table of the Database

ID |     DATA INICIO     |       DATA FIM      |ART_IDART (FK)
4  |"2018-11-30 00:00:00"|"2018-11-20 00:00:00"|  "";  
5  |"2018-11-30 00:00:00"|"2018-11-21 00:00:00"|  ""; 

1 answer

0

At the getter where you are defining the relationship between your fields this:

@OneToOne
public Art getArt() {
    return art;
}

When in fact it should be returning a direct reference to the object that its class models, thus:

@OneToOne
public Art getArt() {
    return this.art;
}

Learn more about how this works see and to understand more how relationships are mapped see

If your application is just for testing, please consider deleting the table and creating it again.

  • Unfortunately, the problem still persists. The foreign key does not go to column ART_IDART

  • 1

    I solved the problem, I just deleted the bank and created it again and things worked

  • I was going to suggest this. Did you ever switch to 'Return this.art;' ? If the answer helped you don’t forget to mark as solved

  • Otávio Reis Perkles, yes, but as I said in the first comment of that reply, I was not successful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.