Why does hibernate not list results if the select query calls null field of the table?

1

I have a Class-Entity Product.

    @Entity
public class Produto implements Serializable{
    @Id
    @GeneratedValue
    private long id;
    @Column(length = 70, nullable = false)
    private String cod;
    @Column(length = 100, nullable = false)
    private String descricao;
    @Column
    @OneToOne
    @JoinColumn(name = "idUnComer")
    private Unidade unComer;//unidade comercial

}

However, when I do the query:

Select prod.id as id,prod.cod as cod,prod.descricao as descricao,prod.unComer as unComer from Produto prod 

It does not list if there is no commercial unit saved. Only entering the values where the foreign key was filled in.

Now if I do this:

from Produto

It lists all the records, having null foreign keys or not.

In fact my product contains several other fields, but I just wanted to list the bank only a few that I need.

Has anyone ever been through this?

I'm using Hibernate 4.3 and mysql 5.6.

    
asked by anonymous 15.09.2016 / 18:57

2 answers

1

To solve I had to do left join in the query, being that when calling prod.unComer was already to do. It's kind of redundant, but it was the solution I had to do select prod.id as prod, prod.cod as cod, prod.descricao as descricao,prod.unComer as unComer from Produto prod left join prod.unComer as unComer

    
21.09.2016 / 15:19
0

Hibernate does not work with primitive types (Hibernate, not working) Java and primitive types, need converters because a primitive type will never be nullo always has a default value

Try to do something

public void recebeInt(int value){
System.out.println(value); }

Now call this method so

Integer meuInt=null;
recebeInt(meuInt);

See what happens!

Always work with objects, not primitive types

Objeto          Primitivo
Character       char
Integer         int
Long            long
Double          double
    
21.09.2016 / 15:41