-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Select case when empty string
PostPosted: Wed Mar 06, 2013 1:37 pm 
Newbie

Joined: Wed Mar 06, 2013 1:05 pm
Posts: 2
I am having problems to implement a search in a PostgreSQl table. The table contains columns with empty strng values that causes an error.

Some information from the column design:
Data type: character varying(20)
Default:
Not NULL: No
Primary key: No

My java class annotation for the column:
@Column(name = "codeClient")
private String codeClient;

This query string works when there is no "empty string" value:
SELECT c FROM Client c WHERE c.id = 765432

And my Json returns:
[{"id":765432,"nameClient":"JACK SMITH","codeClient":"234567890"}]


When I change the query to deal with a empty value:
SELECT c.id, c.nameClient, CASE c.codeClient WHEN '' THEN 'nonexistent' else c.codeClient END FROM Client c WHERE c.id = 765432

The query works, but the Json result misses de columns descriptions:
[[765432,"JACK SMITH","234567890"]]

To solve this problem, I change the query to return an object:
SELECT new ClientDTO(c.id, c.nameClient, CASE c.codeClient WHEN '' THEN 'nonexistent' else c.codeCliente END) FROM Client c WHERE c.id = 765432

But Hibernate can't solve when the result is an empty string:
Java.lang.IllegalArgumentException: org.hibernate.QueryException: could not instatiate class [ClientDTO] from tuple

I've tried to use JPA CriteriaBuilder. It works on simple querys, but due to the complexity, I could not implement a query that I need.

I can't change de database design and I need the Json result.

Anybody can help me?


Top
 Profile  
 
 Post subject: Re: Select case when empty string
PostPosted: Thu Mar 14, 2013 2:36 pm 
Newbie

Joined: Wed Mar 06, 2013 1:05 pm
Posts: 2
Done. I cast the ArrayList result to a POJO object manually.


Client clie;
List<Client> clients = new ArrayList<Client>();

List<Object[]> clientsObj = manager.createQuery(query).getResultList();

for (Object[] itemObj : clientsObj) {
clie = new Client();

clie.setId(Long.parseLong(itemObj[0].toString()));
clie.setNameClient(itemObj[1].toString());
clie.setCodeClient(itemObj[2].toString());

clients.add(clie);
}

return clients;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.