-->
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.  [ 4 posts ] 
Author Message
 Post subject: JPA Hibernate native Query to "unmapped" table
PostPosted: Thu Sep 30, 2010 6:20 am 
Newbie

Joined: Thu Sep 30, 2010 5:32 am
Posts: 4
Hi,

how can I query a table that is not a "mapped entity" through JPA Hibernate?
(there is no POJO annotated with @entity for that table)

I tried entityManager.createNativeQuery(mySQLString). No exception is thrown but I have no idea what type the objects in the returned list have (getResultList()).


Top
 Profile  
 
 Post subject: Re: JPA Hibernate native Query to "unmapped" table
PostPosted: Thu Sep 30, 2010 7:34 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:34 pm
Posts: 24
simply look
usually List<Object[]>


Top
 Profile  
 
 Post subject: Re: JPA Hibernate native Query to "unmapped" table
PostPosted: Thu Sep 30, 2010 9:15 am 
Newbie

Joined: Thu Sep 30, 2010 5:32 am
Posts: 4
maint175 wrote:
simply look
usually List<Object[]>


An Object of type "Object" usually isn't that helpful if I'm interested as Example in an Integer value. I'm more interested in the "real" type. So is an integer column returned as a java Integer? or a java.sql type? -> must be casted.
So questions: is it possible at all?
and if yes what types to cast the returned objects to?


Top
 Profile  
 
 Post subject: Re: JPA Hibernate native Query to "unmapped" table
PostPosted: Thu Sep 30, 2010 10:58 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:34 pm
Posts: 24
/**
* Interface for all result extractors.
*/
interface ResultExtractor<T> {
public T getResult(List resultSet);
}


/**
* Extractor for single integer result. You have to set number of needed
* column in the result set row.
*/
class IntegerExtractor implements ResultExtractor<Integer> {
private final Integer numberOfColumn;

public IntegerExtractor(int numberOfColumn) {
this.numberOfColumn = numberOfColumn;
}

public Integer getResult(List resultSet) {
if (0 < resultSet.size()) {

if (resultSet.get(0) instanceof List) {
// toplink
Object value = resultSet.get(0);
if (value instanceof List) {
return Integer.valueOf(((List) value).get(numberOfColumn).toString());
} else {
return Integer.valueOf(value.toString());
}
} else if (resultSet.get(0).getClass().isArray()) {
// hibernate
Object value = ((Object[]) resultSet.get(0))[numberOfColumn];
return Integer.valueOf(value.toString());
} else {
Object value = resultSet.get(numberOfColumn);
return Integer.valueOf(value.toString());
}
}
return null;
}
}


/**
* abstract DAO method
*/
<T> T findByNativeQuery(final String queryString, ResultExtractor<T> resultExtractor) {
Query query = entityManager.createNativeQuery(queryString);
return resultExtractor.getResult(query.getResultList());
}




and use

Integer result = dao.findByNativeQuery("select num from table", new IntegerExtractor(0));


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.