hql

HQL select range of results

孤者浪人 提交于 2019-12-11 03:28:04
问题 What is the correct/efficient way to retrieve a sub set of results from hibernate? Assuming that products is a table containing a 3000 records. Session session = SessionFactorys.getSessionFactory().openSession(); Query query = session.createQuery("from products p"); List result = query.list().subList(30, 40); session.disconnect(); The code above dose not seem to be very efficient is there a better way, I am trying to find something like. Query query = session.createQuery("from products p

Hibernate Query - Get latest versions by timestamp?

依然范特西╮ 提交于 2019-12-11 02:25:42
问题 I have a database that is being used as a sort of version control system. That is, instead of ever updating any rows, I add a new row with the same information. Each row also contains a version column that is a date timestamp, so the only difference is the new row will have a more recent timestamp. What I'm having trouble with is writing an efficient hibernate query to return the latest version of these rows. For the sake of example, these are rows in a table called Product , the timestamped

How to do custom-query based collection mappings in Hibernate?

守給你的承諾、 提交于 2019-12-11 02:23:50
问题 I have an object that is mapped to have a set of objects, very simple. However, what I really want to do is put some criteria on that mapping. Here's the current mapping: <set name="ops" inverse="true" cascade="all, delete-orphan"> <key column="cityblock_id" on-delete="cascade"/> <one-to-many class="com.tamedtornado.data.Operation"/> </set> Now, instead of just getting all associated ops, I want to just get ops that haven't completed, or have, or whatever. The Hibernate docs are very quiet on

Getting DATEPART in HQL or Criteria?

一世执手 提交于 2019-12-11 02:07:26
问题 How can i get DATEPART of a time using HQL or Criteria ? I have googled it up and get some tips, but wasn't enough. If there is someone who has experienced it before, please let us know. Thanks in advance 回答1: Take a look at this post about using sql functions in nhibernate from ayende. EDIT I have had success with something like this in the configuration file : <property name="query.substitutions"> true 1, false 0, yes 'Y', no 'N',getdate=getdate </property> notice the getdate=getdate, not

HQL joins in Grails

半世苍凉 提交于 2019-12-11 02:05:26
问题 I have a relationship like this class Foo { static hasMany = [bars: Bar, things: Thing] } class Bar { // Has nothing to tie it back to Foo or anything else of importance } class Thing { // Has nothing to tie it back to Foo or anything else of importance } I have an instance of Thing . I want to get all instances of Bar that are associated with all instances of Foo that are associated with the instance of Thing that I have. I have used Grails executeQuery method several times but my query does

HQL Inner join on same table

余生长醉 提交于 2019-12-11 00:19:38
问题 I asked an earlier question today which was about GORM: How to fetch records in grails by max date and group at same time However, someone suggested it can be easily achieved using HQL. But using HQL I'm getting unexpected token error. When I researched this I found out that HQL doesn't allow INNER JOINS unless there is an association between two entities: HQL, left join on the same table So, I'm lost. To begin with, I'm frustrated why such a simple query isn't supported by GORM and now with

HQL: variable column

只谈情不闲聊 提交于 2019-12-10 23:57:41
问题 I'm able to set variable values for "where" restrictives: Query criteria = session.createQuery( "select test.col1, test.col2, test.col3 "from Test test " + "where test.col = :variableValue "); criteria.setInteger("variableValue", 10); But is it possible to set variable column like this? String variableColumn = "test.col1"; Query criteria = session.createQuery( "select test.col1, test.col2, test.col3 "from Test test " + "where :variableColumn = :variableValue "); criteria.setInteger(

Hibernate HQL : no entity found for query

天大地大妈咪最大 提交于 2019-12-10 22:59:47
问题 It spent a day trying solve this problem however it's still not successful. I have Test entity : public class Test { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(name = "duration", nullable = false) private int duration; @Column(name = "test_name", nullable = false, unique = true) private String testName; @Column(name = "archived", nullable = false) private boolean archived; @OneToMany(cascade = CascadeType.ALL, mappedBy = "test", fetch =

Getting data from database using HQL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 22:54:58
问题 Hi, I have database Staff with 3 tables: mysql> describe person; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | person_id | bigint(20) | NO | PRI | NULL | auto_increment | | date | datetime | YES | | NULL | | | first_name | varchar(255) | YES | | NULL | | | last_name | varchar(255) | YES | | NULL | | | position | varchar(255) | YES | | NULL | | |

HQL limit in subselect query

一笑奈何 提交于 2019-12-10 21:47:20
问题 I want to insert foreign key of one table in another on basis of certain criteria. Structure is like insert into CustomerResult(customer,draw) select c.idCustomer, from Customer c,Draw d where ..... and c.idCustomer in (select cc.idCustomer from Customer cc where ..... limit 10) here i want to insert only fix no of records which fulfill certain criteria. I know hql has no limit keyword but want to implement like this. any suggestion? 回答1: I don't think that what you want to do (use limit in