Error in Relationship NoHibernate

Asked

Viewed 29 times

0

I have the following classes

User:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.context.annotation.Scope;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@Table(name = "portal_sega_user")
@Scope(value=WebApplicationContext.SCOPE_SESSION)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "usuario")
public class Usuario {

    public Usuario() {

    }

    public Usuario(String usuario, boolean inativo, String nome, String telefoneFixo, String telefoneCelular,
            String cargo, String email, String centroDeCusto, String localidade, String site, String endCompleto,
            String observacao,Set<Perfil> perfis, Set<Secretaria> secretarias) {
        super();
        this.usuario = usuario;
        this.inativo = inativo;
        this.nome = nome;
        this.telefoneFixo = telefoneFixo;
        this.telefoneCelular = telefoneCelular;
        this.cargo = cargo;
        this.email = email;
        this.centroDeCusto = centroDeCusto;
        this.localidade = localidade;
        this.site = site;
        this.endCompleto = endCompleto;
        this.observacao = observacao;
        this.perfis = perfis;
//        this.secretarias = secretarias;
    }

    @Id
    private String usuario;
    private boolean inativo;
    private String nome;
    private String telefoneFixo;
    private String telefoneFixoOpc;
    private String telefoneCelular;
    private String telefoneCelularOpc;
    private String cargo;
    private String email;
    private String centroDeCusto;
    private String localidade;
    private String site;
    private String endCompleto;
    private String observacao;


    
    
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.DETACH)
    @JoinTable(name = "portal_sega_perfil", joinColumns = { @JoinColumn(name = "usuario_id") }, inverseJoinColumns = {
            @JoinColumn(name = "perfis_id") })
    private Set<Perfil> perfis = new HashSet<>(); // perfis
    
    
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
    private List<String> secretarias;

    
    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public boolean isInativo() {
        return inativo;
    }
    

    public void setInativo(boolean ativo) {
        this.inativo = ativo;
    }
    
    public String getNome() {
        return nome;
    }

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

    public Set<Perfil> getPerfis() {
        return perfis;
    }

    public void setPerfis(Set<Perfil> perfis) {
        this.perfis = perfis;
    }

    public String getTelefoneFixo() {
        return telefoneFixo;
    }

    public void setTelefoneFixo(String telefoneFixo) {
        this.telefoneFixo = telefoneFixo;
    }

    public String getTelefoneCelular() {
        return telefoneCelular;
    }

    public void setTelefoneCelular(String telefoneCelular) {
        this.telefoneCelular = telefoneCelular;
    }
    public String getTelefoneFixoOpc() {
        return telefoneFixoOpc;
    }

    public void setTelefoneFixoOpc(String telefoneFixoOpc) {
        this.telefoneFixoOpc = telefoneFixoOpc;
    }

    public String getTelefoneCelularOpc() {
        return telefoneCelularOpc;
    }

    public void setTelefoneCelularOpc(String telefoneCelularOpc) {
        this.telefoneCelularOpc = telefoneCelularOpc;
    }

    public String getCargo() {
        return cargo;
    }

    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCentroDeCusto() {
        return centroDeCusto;
    }

    public void setCentroDeCusto(String centroDeCusto) {
        this.centroDeCusto = centroDeCusto;
    }

    public String getLocalidade() {
        return localidade;
    }

    public void setLocalidade(String localidade) {
        this.localidade = localidade;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public String getEndCompleto() {
        return endCompleto;
    }

    public void setEndCompleto(String endCompleto) {
        this.endCompleto = endCompleto;
    }

    public String getObservacao() {
        return observacao;
    }

    public void setObservacao(String observacao) {
        this.observacao = observacao;
    }
}

And

Secretariat

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@Table(name = "tb_sega_vip_secretaria")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)
public class Secretaria implements Serializable {
/*
 * chave composta da tabela tb_sega_vip_secretaria
 */
@Id
private String id_vip;
@Id
@OneToMany(mappedBy = "secretarias",fetch = FetchType.EAGER)
private List<String> id_secretaria;

/*
 * tipo de secretaria 1 = primaria 2 = secundaria
 */
private Integer tipo;


public Integer getTipo() {
    return tipo;
}

public void setTipo(Integer tipo) {
    this.tipo = tipo;
}

public String getId_vip() {
    return id_vip;
}

public void setId_vip(String id_vip) {
    this.id_vip = id_vip;
}

public List<String> getId_secretaria() {
    return id_secretaria;
}

public void setId_secretaria(List<String> id_secretaria) {
    this.id_secretaria = id_secretaria;
}

}

And presents the following error:

@OneToOne or @ManyToOne on br.com.claro.tcc.web.portalsega.model.Usuario.secretarias references an unknown entity: java.util.List

Any suggestions for a solution? Thanks in advance.

1 answer

1

In which table is the secretaria_id column? It is in the user table?

In the User Voce class you have:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
private List<String> secretarias;

It should be something like:

@JoinColumn(name="SECRETARIA_ID")
@ManyToOne(fetch = FetchType.LAZY)
private Secretaria secretaria;

In the class Voce Secretariat has:

@OneToMany(mappedBy = "secretarias")
private List<String> id_secretaria;

It should be something like:

@OneToMany(mappedBy = "secretaria")
private List<Usuario> usuarios;

I hope I’ve helped.

  • the secretaria_id is within tb_sega_vip_secretariat, I made the change to receive the object Secretariat, I drew exactly as you proposed, however the error remains, I believe the problem is in the relationship of the tables, something I’m missing pass.

Browser other questions tagged

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