Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set null values in result projection to zero with QueryDSL #305

Closed
carlosell opened this issue Nov 30, 2012 · 4 comments
Closed

How to set null values in result projection to zero with QueryDSL #305

carlosell opened this issue Nov 30, 2012 · 4 comments
Milestone

Comments

@carlosell
Copy link

@carlosell carlosell commented Nov 30, 2012

Timo suggested me to report this incidence as a bug:

I like to know how to set the null value in the projection pronostico2.nropack.sum()...

new HibernateSubQuery().from(pronostico)
.where(pronostico.aeropuerto.eq(vuelo.aeropuertoDestino)
.and(pronostico.fechareg.before(fechaHoraSimulacion)))
.unique(pronostico.nropack.sum()).loe(aeropuerto.cappack.subtract(vuelo.numpack))))

When the pronostico table is empty in DB, the value returned in pronostico.nropack.sum() is null, but i want to be zero. I tried with coalesce like this: pronostico.nropack.sum().coalesce(0)

.. but i get no answer. Instead of that, i get a Cast Exception: Cannot convert Long to Integer...

Someone has the answer?

PD: The complete query is:

QVuelo vuelo = QVuelo.vuelo;
QAvion avion = QAvion.avion;
QTipoAvion tipoAvion = QTipoAvion.tipoAvion;
QAeropuerto aeropuerto = QAeropuerto.aeropuerto;
QPronostico pronostico = QPronostico.pronostico;
QPronostico pronostico2 = new QPronostico("pronostico2");

List<Vuelo> vuelos = consulta.from(vuelo)
                        .innerJoin(vuelo.avion, avion)
                        .innerJoin(avion.tipoAvion, tipoAvion)
                        .innerJoin(vuelo.aeropuertoDestino, aeropuerto)
                        .where(vuelo.aeropuertoOrigen.idaeropuerto.eq(idAeropuertoActual)
                                .and(vuelo.salidaest.after(fechaHoraSimulacion))
                                .and((vuelo.numpack.add(nroPaquetes).loe(tipoAvion.cappack)))
                                .and(new HibernateSubQuery().from(pronostico)
                                        .where(pronostico.aeropuerto.eq(vuelo.aeropuertoDestino)
                                                .and(pronostico.fechareg.before(fechaHoraSimulacion)))
                                        .unique(pronostico.nropack.sum()).loe(aeropuerto.cappack.subtract(vuelo.numpack))))
                        .list(vuelo);       

Extracted from my original question in http://stackoverflow.com/questions/13319628/how-to-set-null-values-in-result-projection-to-zero-with-querydsl.

timowest added a commit that referenced this issue Dec 2, 2012
@timowest
Copy link
Member

@timowest timowest commented Dec 2, 2012

Just to be sure, you are using the latest version of Querydsl 2.9.0? I wasn't able to reproduce this directly with a simpler query.

@timowest
Copy link
Member

@timowest timowest commented Dec 2, 2012

Now I got it reproduced.

timowest added a commit that referenced this issue Dec 2, 2012
@carlosell
Copy link
Author

@carlosell carlosell commented Dec 10, 2012

Thanks, Timo. QueryDSL framework help me a lot for my undergraduate project, especifically the implementation of simulated annealing in Java for a package delivery software system.

Greetings from Lima, Peru.

PD: I didn't test the patch because the course has been completed, but it will be useful for future development to other people. Thanks again.

@timowest
Copy link
Member

@timowest timowest commented Jan 13, 2013

Released in 3.0.0.BETA1

@timowest timowest closed this Jan 13, 2013
@timowest timowest added this to the 3.0.0 milestone Apr 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants