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

Asked

Viewed 224 times

1

I have a Product Entity-Class.

    @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 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.Bringing in the query only the values where the foreign key was filled.

If I do so:

from Produto

It lists all registrations, having foreign keys null or not.

Actually my Product, contains several other fields, only I wanted to list from the bank only a few that I need.

Someone’s been through it?

I am using Hibernate 4.3 and mysql 5.6.

  • Your query presents errors in stacktrace, publish in the question!? vc does not do the prod.descricao and its entity does not possess the attribute partGer .

  • Does not generate errors. Simply does not list if you have null in relational attributes, such as business unit. To resolve I had to do left Join in the query, and when calling Prod.unComer it was already to do. It’s kind of redundant, but it was the solution I had to make 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

  • Marlucio Pires. Cool, just add the answer to close the question.

2 answers

1


To resolve I had to do left Jun in the query, and when calling Prod.unComer was already to do. It’s kind of redundant, but it was the solution I had to make 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

0

Hibernate does not work with primitive types (hibernate, do not work) Java and primitive types need converters because a primitive type will never be null 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
  • Are you saying that if in the place of long I used Long it would bring the commercial unit without having to do the left Join?

  • Yes, if you have no query restrictions it brings, if you do not list it is because its field mapping properties are mapped incorrectly

  • You haven’t changed at all... but you see the intention

  • Used on the father-son object?

Browser other questions tagged

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