Error when relating two tables in the database

Asked

Viewed 44 times

0

I’m with a web project in java, in which I’m mapping towers and patchpanels, where a tower has several patchs and a patch has only one tower attached to it. This relationship is all ok, now the port-to-patch relationship, where a patch has multiple ports and a port has only one patch linked to it giving error. When registering new patch and adding equipment the ports it saves the equipment but does not link to which patch panel it is linked. As you can see below, where description would be the equipment connected to the door:

inserir a descrição da imagem aqui

My patch panel model: `package models;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import play.data.validation.Required;
import play.data.validation.Unique;
import play.db.jpa.Model;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
public class Patchpanel extends Model {

@Required
public String nome;
@Required
public String mac;
@Required
@Unique
public String ip;
@Required
public String numPortas;

@OneToMany(cascade={CascadeType.ALL}, mappedBy="patchpanels")
public List<Porta> portas;

@ManyToOne
@JoinColumn(name="torre_id")
public Torre torre;

@Enumerated(EnumType.STRING)
public Status status;

public Patchpanel() {
    status = Status.ATIVO;
}
}

` my model doors:

package models;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
public class Porta extends Model {

@Required
public String descricao;

@Enumerated(EnumType.STRING)
public Status status;

@ManyToOne
public Patchpanel patchpanels;

public Porta() {
    status = Status.ATIVO;
}
}

My patchpanels controller where the patch is saved:

public static void salvarPatchpanel(@Valid Patchpanel patchpanel, List<String> portas) {

    if (validation.hasErrors()) {
        params.flash();
        validation.keep();
        formPatchpanel();
    }

    for (String porta : portas) {
        patchpanel.portas = new ArrayList<Porta>();
        Porta p = new Porta();
        p.descricao = porta;
        patchpanel.portas.add(p);
        patchpanel.save();
    }
    String mensagem = "Cadastro realizado com sucesso!";
    flash.success(mensagem);
    //patchpanel.save();
    listagemPatchpanel(null);

}
  • Is there anything in the log? Or it just saves as null?

  • Nothing in the log, just saved as Null

1 answer

0


Isn’t one missing @JoinColumn(name="NOME_DA_COLUNA") in this code?

@ManyToOne
public Patchpanel patchpanels;

Browser other questions tagged

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