hql

HQL select sum and time group by time unit (hour/day/month) and

ε祈祈猫儿з 提交于 2020-01-04 05:20:35
问题 I have a table like this: COLUMN TYPE ------------------------------ ID INTEGER VALUE INTEGER TIME TIMESTAMP How can I write query with HQL selecting sum of value column groupped by time unit(f.e. groupped by day) and selecting this time unit as a second column. I have tried to do it and got something like this: but there is no parsedatetime function in HQL so I have no idea how can I get the proper query now. select sum(value), parsedatetime(day(time) || '.' || month(time) || '.' || year

Get random object from SQL database via Hibernate

和自甴很熟 提交于 2020-01-04 04:59:06
问题 I have following MySql dependent code ( ORDER BY RAND() ) . I would like to know if there is hibernate HQL alternative for it (admin is boolean tag indicating that the user as an admin). This is working code: public long getRandomAdmin() { Session session = getSession(); Query selectQuery = session.createSQLQuery("SELECT user_id FROM users WHERE admin = '1' ORDER BY RAND()"); selectQuery.setMaxResults(1); List<BigInteger> list = null; try { list = selectQuery.list(); } catch

hql join @CollectionTable

北城余情 提交于 2020-01-03 20:04:35
问题 I have a domain Service with collection tags as below : @Entity public class Service extends AbstractEntity<Long> { private static final long serialVersionUID = 9116959642944725990L; @ElementCollection(fetch = FetchType.EAGER, targetClass = java.lang.String.class) @CollectionTable(name = "service_tags", joinColumns = @JoinColumn(name = "s_id")) @Column(name = "tag") private Set<String> tags; } I want to select Service s with particular KEY of Service.tags . hql joining Service to Service.tags

How to limit result in @Query used in Spring Data Repository

﹥>﹥吖頭↗ 提交于 2020-01-03 15:20:31
问题 I am retrieving data by CrudRepository in Spring Data JPA . I want to filter my records those are retrieved from my custom query provided in @Query annotation. I tried .setMaxResults(20); for select rows.. But it gives errors. I want to filter first 20 rows from my table this is my repository package lk.slsi.repository; import java.util.Date; import lk.slsi.domain.SLSNotification; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository;

What is the Hibernate equivalent of “select 1 from DUAL”?

浪子不回头ぞ 提交于 2020-01-03 11:57:10
问题 What is the minimal Hibernate query? Is there an equivalent of "select 1 from DUAL" for Hibernate? I am writing a healthcheck function and would like to execute a minimal query, but use HQL to keep the code portable (as opposed to using a native query). 回答1: Hibernate's dialects are what translate to the various databases, and there's no "health check" or "validation" query support in the dialects. This kind of thing is normally done by the connection pool, not by Hibernate. See, e.g., DBCP's

HQL: combine “insert into … select” with fixed parameters values

前提是你 提交于 2020-01-03 11:32:09
问题 I have HQL statement: insert into Item (ost, value, comments, startTime, endTime, proposedBy) select si.ost, si.value, si.comments, si.endTime, si.endTime, u from Item si, User u where si.ost = ? and u.id = ? How could it be modified to use parameters' values for ost and startTime columns while taking other columns from select ? 回答1: Can’t be done in HQL; it doesn’t allow parameter references in the select clause. 回答2: I don't know about that last answer. I am using NH 3.2 and I was able to

How to use the setParameterList() method in Hibernate?

别等时光非礼了梦想. 提交于 2020-01-03 07:40:42
问题 I have a requirement to fetch selected rows from Oracle database based on ids supplied as an array, something like the SELECT ... FROM table_name WHERE id IN() query. In my attempts to do so, I'm trying to use the org.hibernate.setParameterList(String name, Object[] values) method in my DAO as follows. @Service @Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW) public final class ProductImageDAO implements ProductImageService { @SuppressWarnings("unchecked") public List

I can't make a inner join between two tables in hibernate hql query

南笙酒味 提交于 2020-01-02 16:19:54
问题 I am new with this. Please help me. My inner join looks like this: select p.idprodus, p.denumire, p.cantitate from Produs p inner join Furnizor f on p.idfurn = f.idfurn I want to make the inner join on the column idfurn, but I get these errors: org.hibernate.QueryException: outer or full join must be followed by path expression select p.idprodus, p.denumire, p.cantitate from sakila.entity.Produs p inner join Furnizor f on p.idfurn = f.idfurn at org.hibernate.hql.classic.FromParser.token

Indexed element access in JPQL

拟墨画扇 提交于 2020-01-02 08:09:12
问题 Is it possible to do indexed element access in JPQL like in HQL : select o from Order o where o.items[0].id = 1234 I couldn't find something related in the JPA 2 specs, I am targeting EclipseLink JPA here, so if you come up with an EclipseLink solution, that's ok as well, although a JPQL standard solution is preferred. 回答1: The INDEX function should do the trick (actually I tested it and it does): SELECT o FROM Order o JOIN o.items i WHERE i.id = 1234 AND INDEX(i) = 0 From the JPA 2.0

Hibernate Criteria API equivalent to HQL select clause?

可紊 提交于 2020-01-02 05:08:06
问题 I'd like to have a combined query for two persistent classes. In HQL this could be achieved by the select clause, select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr In the above example, Family is a conbined class with DemesticCat as its construtor params What is the Criteria equivalent of the HQL select clause ? 回答1: You'll have to use a ResultTransformer for this. The Hibernate 3.2: Transformers for HQL and SQL blog