hql

NHibernate Polymorphic Query on a Collection

I'm trying to write a query in NHibernate. I don't really care if I use the Criteria API or HQL, I just can't figure out how to write the query. Here's my model: public class LogEntry { public DateTime TimeCreated { get; set; } } public class Note : LogEntry { public string Content { get; set; } } public class Workflow { public IList<...

NHibernate How to Select distinct objects based on specific property using HQL?

How can HQL be used to select specific objects that meet a certain criteria? We've tried the following to generate a list of top ten subscribed RSS feeds (where SubscriptionCount is a derived property): var topTen = UoW.Session.CreateQuery( @"SELECT distinct rss FROM RssFeedSubscription...

Searching object property in a predefined list using HQL

Hello, I'm trying to make a query in HQL that see if the id of a person is in a list of predefined ids. For example, I would like to find all persons that have id 1 or 2 in a database. The problem is that I cannot do: from Person person where id in elements(:ids) because elements expects an identifier (like person.childIds for example...

HQL 'parsename' equivalent

I've discovered PARSENAME function as a good choice to order IP address stored in Database. Here there is an example. My issue is I'm using Hibernate with named queries in a xml mapping file and I am trying to avoid the use of session.createSQLQuery(..) function. I'm wondering if exists any PARSENAME equivalent function for HQL querie...

Getting latest entries for each entity using HQL?

I'm having some problems with getting NHibernates HQL to behave the way I want. I have two tables, lets call them X and XLOG. There are multiple XLOG entries for each X. X: - Id XLOG: - Id - X_Id - Timestamp I want to write an HQL query that will produce the latest XLOG entry for each X. In SQL, I could either make a correlated...

Hibernate is much faster when executing a native query.

The following query is supposed to return about 800 objects. The problem is that hibernate actually executes 800 queries to get them all. It appears to execute one query to get the ids and then executes one query for every object to get the specific data about the object. It takes over 60 seconds for this query to return. List<AUser> re...

nhibernate query to return a User by his Guid

I have a user class: public class User { public virtual int ID {get;set;} public virtual string UserGuid {get;set;} // its unique! } Can someone show me how to query using HQL and criteria to get the user by UserGuid? ...

Custom SQL function for NHibernate dialect

I want to be able to call a custom function called "recent_date" as part of my HQL. Like this: [Date] >= recent_date() I created a new dialect, inheriting from MsSql2000Dialect and specified the dialect for my configuration. public class NordicMsSql2000Dialect : MsSql2000Dialect { public NordicMsSql2000Dialect() { Register...

How to query a date in HQL (Hibernate) with Joda Time?

I am sure that someone familiar with HQL (I am myself a newbie) can easily answer this question. In my Grails application, I have the following domain class. class Book { org.joda.time.DateTime releaseDate //I use the PersistentDateTime for persisting via Hibernate (that use a DATETIME type for MySQL DB) } In my HQL query, I want t...

Hibernate: find duplicates

Hi, Assume I have the following Groovy class (or the equivalent in Java) class User { Long id String name } I would like to write a Hibernate query (either HQL or Criteria) which returns all the users that have at least one other user with the same name. Update The following query has been suggested select min(user.id), u...

Avoiding NULL object in HQL Query where clause

I have an entity which might have a parent entity. I want to run this query: select entity where entity.parent.id = 9 some of the entity does not have parents (entity.parent = null) and N HIBERNATE Fails to run this query (QueryException - Could not resolve property) How can I use HQL to get all the entities that has parents entities ...

HQL : Getting the primary key of an object.

Hi , I am using HQL to save an object in the DB. It is something like this Query query = sessionFactory.getCurrentSession().createQuery(ASK_FOR_HELP_SAVE_QUERY); query.setInteger("status", askForHelp.getStatus()); query.setString("requestId", askForHelp.getRequestId()); query.setString("toAccountId", askForHelp...

Escape SQL Parameter

I need to work around an NHibernate bug I reported here. Essentially, NHibernate will generate multiple SQL parameters for the same HQL parameter, which results in problems for queries that use a parameter in a grouping construct. Until the bug is fixed, I figured I'd just concatenate the parameter into the HQL. Obviously this is suscep...

Hibernate and dry-running HQL queries statically

I'd like to "dry-run" Hibernate HQL queries. That is I'd like to know what actual SQL queries Hibernate will execute from given HQL query without actually executing the HQL query against real database. I have access to hibernate mapping for tables, the HQL query string, the dialect for my database. I have also access to database if that...

HQL - can i join when the association is backwards?

I have: class A { B b; } class B { } I know i can do this: from A a join a.b b but what I need to do is this (pseudo-HQL, it doesn't parse, hence this post): from B b left outer join A a on a.b = b I get "Path expected for join!" :( I want a complete list of Bs joined onto any As, if they exist. Is this possible? I can't ...

Update one value from a list of dependent objects

Given an entity with a list of components: class Entity{ Long id; String name; List<Component> components = new ArrayList<Component>(); } class Component{ Object value; } Configuration: <hibernate-mapping> <class name="Entity" table="entity"> <id name="id" access="field" column="id"/> <property name="...

nhibernate hql with named parameter

I have implemented a search function using Castel Active Record. I thought the code is simple enough but I kept getting NHibernate.QueryParameterException : could not locate named parameter [searchKeyWords] errors. Can someone tell me what went wrong? Thanks a million. public List<Seller> GetSellersWithEmail(string searchKeyWords) ...

Hibernate HQL query by using like operator

Hi, Seu the following mapping @Entity public class User { private Integer id; @Id; private Integer getId() { return this.id; } } Notice id is an Integer. Now i need this HQL query by using like operator Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.id like :userId"); A...

nhibernate hql subquery performance

I have written an hql to support paging string hql = @"select distinct mr from MediaResource as mr where mr.Deleted= false and mr.Type = :typeId"; SimpleQuery<MediaResource> q = new SimpleQuery<MediaResource>(hql); q.Se...

NHibernate + Fluent NHibernate + Oracle Index

I have a table with more than 10 000 000 rows. In TOAD this query works very well on it: select /*+ INDEX(x IDX_CASHFLOW_COMPLEX)*/ * from MYPR.CASHFLOW x where fk_debet in (21856, 21854, 21855) IDX_CASHFLOW_COMPLEX is index on 5 columns created by following script: CREATE INDEX MYPR.IDX_CASHFLOW_COMPLEX ON MYPR.CASHFLOW (FK_DEBIT...