users@glassfish.java.net

Columns with NULL value and EJB3 queries containing ORDERBY clause.

From: <glassfish_at_javadesktop.org>
Date: Thu, 10 Apr 2008 07:57:10 PDT

I´m using a stateless session bean and entity classes to access the data in a MySQL Database from an external Swing application.
What I have is:


EntityManager em;
...

I have an entity class named AlarmaH to map a DB table.
In this entity I have an object field named "user" in the form:

@ManyToOne
@JoinColumn(name = "USER", referencedColumnName = "ID")
private User user;

and finally in the entity class User, among other fields, I have one named "name" defined by:

private String name;

and then the get/set functions:

@Column(nullable=false, length=32)
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

It is important to say that the data in the Database column mapped by "user" contains some NULL values.

If I now create the query...

javax.persistence.Query ejbQuery = em.createQuery("SELECT a FROM AlarmaH a WHERE a.user = ?1 or a.user = ?2");
ejbQuery.setParameter(1, some_user_object_1);
ejbQuery.setParameter(2, some_user_object_2);
ejbQuery.setMaxResults(some_limit);
ejbQuery.setFirstResult(some_first_register);

...and then i obtain the result...

LinkedHashSet<AlarmaH> result = new LinkedHashSet<AlarmaH>();
for(Object o : ejbQuery.getResultList())
result.add((AlarmaH)o);

that query retrieves the data correctly (it gives me both NULL and not NULL values), but if in the same query I now add an ORDERBY clause at the end:

javax.persistence.Query ejbQuery = em.createQuery("SELECT a FROM AlarmaH a WHERE a.user = ?1 or a.user = ?2 ORDER BY a.user.name ASC")...

then the NULL values disappear from the result of the query, just as if they were have been filtered out.
I thought that NULL values should be included in the result of an EJB query, no matter if you add an ORDERBY or not.
This kind of problem does not exist with TemporalType.TIMESTAMP columns and Date types, which also include NULL values. In that case, the queries with and without ORDERBY clauses return the same amount of data.

I would appreciate any kind of help, thanks.

Best regards,
David.
[Message sent by forum member 'dvacasp' (dvacasp)]

http://forums.java.net/jive/thread.jspa?messageID=268570