Select in Hibernate does not return extended class

Asked

Viewed 118 times

3

I have a class, Vendor, which is extended from class Pessoa.

In the method listarFornecedores class FOrnecedorDao:

public List<Fornecedor> listarFornecedores() {
    session = HibernateUtil.getSessionFactory().openSession();
    List<Fornecedor> listaFornecedores = new ArrayList<Fornecedor>();
    query = session.createQuery("FROM Fornecedor");
    listaFornecedores = query.list();
    session.close();
    return listaFornecedores;

    }

the result is:

[Person [id=1, name=123, phone=123, address=123, numeroDoEndereco=123, cep=123, bairro=123, cidade=123, estado=123, email=123]]

When rotating the Servlet:

protected void buscar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try{
List<Fornecedor> listaFornecedores = new FornecedorDao().listarFornecedores();

Integer i = 0;
Fornecedor f = new Fornecedor();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

while ( i < listaFornecedores.size() ) {
    f.setid(listaFornecedores.get(i).getid());
    f.setInicioAtividades(listaFornecedores.get(i).getInicioAtividades());  
        String data1 =  sdf.format(f.getInicioAtividades());
        System.out.println(listaFornecedores);
        //System.out.println(data1);
        //System.out.println(f.getInicioFormatado());

        i=i+1;
    }

That is, it only returns the variables of the Person class, and none of the Vendor class... but no jsp is right....

inserir a descrição da imagem aqui

Classe Pessoa:

public abstract class Pessoa implements Serializable{
@Id
@GeneratedValue(strategy= GenerationType.TABLE)
@Column
private Integer id;
@Column
private String nome;
@Column
private String telefone;
@Column
private String endereco;
@Column
private String numeroDoEndereco;
@Column
private String cep;
@Column
private String bairro;
@Column 
private String cidade;
@Column
private String estado;
@Column
private String email;
// resto do código omitido

Supplier Class:

public class Fornecedor extends Pessoa{

    @Column
    private Integer codigo;
    @Column
    private String pessoaContato;
    @Column
    private String cnpj;
    @Column
    private Date inicioAtividades;
    /*Getters and Setters*/
}

Can someone give me a light, explain why this happens, and how we should also take the values of the supplier class?

  • You have placed the Annotations-@Entity @Table(name="Vendor") - in which class?

  • In the Supplier class.

  • You implemented the method toString() in class Fornecedor and wrote it down with @Override?

  • Kra, really the problem was in toString()! It was not in the Vendor class, so I added the following in the Vendor class: @Override public String toString() { Super.toString() + "Supplier [code=" + code + ", person Contact=" + person Contact + ", cnpj=" + cnpj + ", homeactivities=" + homeactivities + "]"; }

2 answers

1


the problem is that I had forgotten the toString of the Supplier class, just entered in the Person class.

So, in the supplier class, I did this:

@Override
public String toString() {

    return super.toString() + "Fornecedor [codigo=" + codigo + ", pessoaContato=" + pessoaContato + ", cnpj=" + cnpj
            + ", inicioAtividades=" + inicioAtividades + "]";
}

The result was:

[Pessoa [id=1, nome=123, telefone=123, endereco=123, numeroDoEndereco=123, cep=123, bairro=123, cidade=123, estado=123, email=123]Supplier [codigo=123, personContact=123, cnpj=123, homeactivities=1986-04-30 16:15:44.0]]

I thank my friend @Felipe Marinho who enlightened my mind :)

0

The two classes will have the annotation Entity. However, the parent class must have a annotation extra indicating what will be the inheritance strategy used.

For example, you may want that for each sub-class a table is created, then you will use:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Pessoa implements Serializable {
}

You can read more about the annotation @Inheritance in this link.

  • Good morning :)! but you already have that note. @Entity @Table(name = "person") @Inheritance(Strategy = Inheritancetype.TABLE_PER_CLASS) public class Person Implements Serializable{

Browser other questions tagged

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