hql

Order By Rand by Time (HQL)

谁说我不能喝 提交于 2019-12-30 07:54:46
问题 I'm developing a web application using asp.net Mvc 2 and NHibernate, and I'm paging data (products in a category) in my page, but this data are random, so, I'm using a HQL statement link this: string hql = "from Product p where p.Category.Id=:IdCategory order by rand()"; It's working fine, but when I page, sometimes the same product appears in the first, second, etc... pages because it's order by rand(). Is there any way to make a random order by fixed by period (time internal) ? Or any

Set array of parameters to hibernate query language

穿精又带淫゛_ 提交于 2019-12-30 04:35:06
问题 Currently the query takes in a single reportID to return the results. Now if I want to pass multiple reportIDs and return the o/p in just 1 call to the DB, how do I do that? String queryText = "from com.abc.domain.bcd.Report report where report.reportID in :reportId"; Query query = SessionFactory.getCurrentSession().createQuery(queryText.toString()); query.setParameter("reportID", reportId); query.list(); I tried passing as an arrayList but no luck. Got the error below List<String> reportID=

Cannot convert '0000-00-00 00:00:00' to TIMESTAMP

删除回忆录丶 提交于 2019-12-30 02:34:07
问题 the field definition /** Date. */ @Column(columnDefinition = "datetime") private Date date; setter public void setDate(final Date date) { DateFormat dfmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { this.date = dfmt.parse(dfmt.format(date)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Does anyone have idea how to convert "zero date" into proper value ? Because i have error: Cannot convert value '0000-00-00 00:00:00' from column 13 to

Hibernate HQL join fetch not recursively fetching

六眼飞鱼酱① 提交于 2019-12-30 01:55:30
问题 I have the following query and method private static final String FIND = "SELECT DISTINCT domain FROM Domain domain LEFT OUTER JOIN FETCH domain.operators LEFT OUTER JOIN FETCH domain.networkCodes WHERE domain.domainId = :domainId"; @Override public Domain find(Long domainId) { Query query = getCurrentSession().createQuery(FIND); query.setLong("domainId", domainId); return (Domain) query.uniqueResult(); } With Domain as @Entity @Table public class Domain { @Id @GenericGenerator(name =

Hibernate new keyword with distinct

浪尽此生 提交于 2019-12-29 06:43:34
问题 I need to take hql that is currently : select distinct a from Asset as a where ... and change it to select new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a where ... My problem is with the distinct keyword. Where does it belong in an hql query where you're using the new Object query type. One thought was to use a sub-select and have my distinct there. I've tried adding distinct a.id but that doesn't work. 回答1: Ok for anyone interested the proper syntax is select distinct new

Hibernate new keyword with distinct

可紊 提交于 2019-12-29 06:43:28
问题 I need to take hql that is currently : select distinct a from Asset as a where ... and change it to select new com.org.AssetDTO(a.id, a.address, a.status) from Asset as a where ... My problem is with the distinct keyword. Where does it belong in an hql query where you're using the new Object query type. One thought was to use a sub-select and have my distinct there. I've tried adding distinct a.id but that doesn't work. 回答1: Ok for anyone interested the proper syntax is select distinct new

“where exists” in Hibernate HQL

核能气质少年 提交于 2019-12-29 06:34:39
问题 How can I write a "not exists" query in HQL? I am trying to get an HQL not exists query which returns the same results as this Oracle SQL query: select * from SCHOOL a where not exists (select 1 from STUDENT b where B.SCHOOL_ID=a.id and B.STATUS_ID not in (0,1,2,3,4)) My mapping files are below: <!-- primary key ommitted --> <set name="students" cascade="all" fetch="select" lazy="false" > <key column="SCHOOL_ID" /> <one-to-many class="com.companyname.Student" /> </set> </class> <!-- primary

Is this possible: JPA/Hibernate query with list property in result?

时光毁灭记忆、已成空白 提交于 2019-12-29 04:18:26
问题 In hibernate I want to run this JPQL / HQL query: select new org.test.userDTO( u.id, u.name, u.securityRoles) FROM User u WHERE u.name = :name userDTO class: public class UserDTO { private Integer id; private String name; private List<SecurityRole> securityRoles; public UserDTO(Integer id, String name, List<SecurityRole> securityRoles) { this.id = id; this.name = name; this.securityRoles = securityRoles; } ...getters and setters... } User Entity: @Entity public class User { @id private

Ordering results by computed value in Hibernate

旧街凉风 提交于 2019-12-28 15:35:51
问题 I have a table Player with columns id, name, wins, games_played. I mapped it to a class Player. I want to do the following query in Hibernate (preferably with Criteria, if not possible with Criteria HQL will also help) select * from Player order by (wins / games_played) I expect to get List<Player> sorted by their win ratio. Thanks for the answer Palo 回答1: Hibernate doesn't support arithmetics expressions in the order by clause. Quoting the section 14.12. The group by clause of the Hibernate

How to set a limit to inner query in Hibernate?

二次信任 提交于 2019-12-28 06:56:44
问题 I have HQL like this: from Table1 t1 where t1.name not in (select t2.name from Table2 t2 order by t2.date limit 10) The problem is it doesn't understand limit keyword. Is there a way to run such query without splitting it into two subqueries? 回答1: look at How do you do a limit query in HQL? you can't limit a query written in hql with hql. You need to make a call to setMaxResults on the Query object, which i guess will prevent you from applying a limit on a hql subquery. This leave you with