Select in Hibernate does not return extended class

3

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

In method listarFornecedores of 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,   eDecemberNumber = 123, zip = 123, neighborhood = 123, city = 123, state = 123,   email = 123]]

When you run 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 class Person, and nothing of the class Provider ... however in no jsp it leaves right ....

Class Person:

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

Vendor 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 to me why this happens, and how should we also pick up the values of the supplier class too?

    
asked by anonymous 23.01.2017 / 13:39

2 answers

1

The problem is that I had forgotten the toString of the Vendor class, I just inserted it into the Person class.

So, in the vendor class, I did so:

@Override
public String toString() {

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

The result was:

  

[Person [id = 1, name = 123, phone = 123, address = 123, eDecemberNumber = 123, zip = 123, neighborhood = 123, city = 123, 123, personContact = 123, cnpj = 123, startActivities = 1986-04-30 16: 15: 44.0]]

I thank my friend @Felipe Marinho that lit my mind:)

    
24.01.2017 / 13:37
0

Both classes will have annotation Entity . However, the parent class must have an extra annotation tect indicating the inheritance strategy used.

For example, you may want a table to be created for each subclass , then you will use:

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

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

    
23.01.2017 / 22:22