I have a dynamic query that is being generated in base of some data passed to a function. For this reason, I don't actually know how many columns I will have in my result. (The query is a pivot of Oracle 11G).
I know that all the generated columns will be numeric items, there's a fixed column that will be always a string.
How can i get a map<String, List<Double>> from hibernate mapping?
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select
Just create select in HQL and get list of map.
As a example:
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) from Cat cat
Using native query tutorial:
http://www.flexjunk.com/2008/03/07/native-sql-in-hibernate/
Related
as a newbie I want to sum the values of a column pv from a database table evm in my model and store it in a variable. I have tried the SQL code SELECT SUM(pv) FROM evm; but that doesn't seem to work.I would be grateful if you lend me an aid regarding how to pull this one.
You can always write a native query and get the response in the resultset to populate the field of your pojo. Once you have the POJO/DTO created as the list of result set perform your sum on the field by Iterating the list.
You do just use the SQL you have suggested. (The database in an AnyLogic model is a standard HSQLDB database which supports this SQL syntax.)
The simplest way to execute it is to use AnyLogic's in-built functions for such queries (as would be produced by the Insert Database Query wizard), so
mySumVariable = selectFirstValue("SELECT SUM(pv) FROM evm;");
You didn't say what errors you had; obviously the table and column has to exist (and the column you're summing needs to be numeric, though NULLs are OK), as does the variable you're assigning the sum to.
If you wanted to do this in a way which more easily fits one of the standard query 'forms' suggested by the wizard (i.e., not having to know particular SQL syntax) you could just adapt the "Iterate over returned rows and do something" code to 'explicitly' sum the columns; e.g., (using the Query DSL format this time):
List<Tuple> rows = selectFrom(evm).list();
for (Tuple row : rows) {
mySumVariable += row.get(evm.pv);
}
I have an instance of JPAQuery<?> and need to retrieve the count. However, since the table may contain many items (millions), I want to limit the count to a given maximum, say 50,000.
The current QueryDSL-Code effectively does this:
query.fetchCount();
Now my desired modifications are quite trivial in raw sql:
select count(*) from (<whatever query> limit 50000);
However, I do not know how I would express this in querydsl. The following code is not correct, because .from() takes an entity path, but query is a query:
JPAExpressions.select(Wildcard.all)
.from(query.limit(50000))
.fetchCount();
I am using querydsl 4.
JPAExpressions.select(Wildcard.all) returns a child of SimplyQuery, which you can call limit on.
JPAExpressions.select(Wildcard.all)
.from(entity)
.limit(50000)
.fetchCount();
we know hibernate has this in clause:
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.in(userIds));
Is there any limit on the size of userIds (which is an ArrayList, say)?
Thanks
It actually depends on the particular database you use. For example in Oracle this limit is 1000.
If you need to pass more values you need to use another approach. For example put the values into a temporary table and then do a select where id in (select id from temptable) query.
I have two tables that I want to join using hibernate. The join column, as represented in my model is a String (its a varchar(10) in my database). When I run the HQL query, what I see is the following error, "conversion failed when converting the varchar value 'AS00' to data type int. "AS500" is the first value of join column in the first row.
I do not know why hibernate is doing this. My join column is not an int. I have checked both models corresponding to my tables and they are both defined as Strings. Is there some kind of restriction on the data types that can be used for join columns?
Please post both your model and the hql query.
If I had to take a guess (and that's all any of us can do without specifics), I would say that your hql query does not use .setParameter and it does not have single quotes around the string value in your query... so it is trying to implicitly convert the value to int.
Example that would cause this error:
Query query = session.createQuery("from Person where name = bob");
I have an entity that has few fields. One of them is city name. Now I want to get list of all distinct cities from that table. How can I archive that. I tried using DISTINCT keyword, but it doesn't work.
I'm using Hibernate as JPA provider but I would like to get it in pure JPA Query.
Have you tried:
SELECT DISTINCT t.city FROM MyTable t