hql

NHibernate QueryOver with sub-query and alias

我怕爱的太早我们不能终老 提交于 2019-12-05 11:46:40
I'm struggling to convert the following (simplified) HQL to QueryOver: select subscription from Subscription as subscription where not exists ( from Shipment as shipment where shipment.Subscription = subscription and (shipment.DeliveryDate = :deliveryDate) ) I've come this far: Subscription subscription = null; Session.QueryOver(() => subscription) .Where(Subqueries.NotExists(QueryOver.Of<Shipment>() .Where(shipment => shipment.Subscription == subscription) .And(shipment=> shipment.DeliveryDate == deliveryDate) .Select(shipment => shipment.Id).DetachedCriteria)); .TransformUsing(new

Hibernate Criteria API equivalent to HQL select clause?

霸气de小男生 提交于 2019-12-05 11:08:55
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 ? You'll have to use a ResultTransformer for this. The Hibernate 3.2: Transformers for HQL and SQL blog post gives the following example (where StudentDTO is a non-entity Bean): List resultWithAliasedBean = s

HQL: order by property of nullable property

久未见 提交于 2019-12-05 10:18:25
Assuming two tables, A[a_id, b_id] and B[b_id,c] . I need to execute the HQL query of form "From A a ORDER BY a.b.c" , while b is nullable in class A . The query,however, returns only instances of A which have non-null b property. This happens because Hibernate generates SQL of form "SELECT FROM A,B WHERE A.b_id = B.b_id ORDER BY B.c" What is the way to return all instances of A with those having null in b to appear first/last? KLE What about : from A a left join a.b_fk b order by b.c The left join takes care of making the join, even if the b_fk property on the java entity (not the table) is

HQL to SQL converter

爷,独闯天下 提交于 2019-12-05 09:17:21
Does anyone know how to convert NHibernate HQL to SQL Scripts? Johannes Rudolph Since HQL translation depends on your mappings and also runtime behaviour, I think it is unlikely there is a way to do so statically. You could run the HQL against a real database and capture the generated SQL either via a profiler for your specific rdbms or NHProf. My old trainings. That was beta-version. Here it is! (hql2sql.jsp) <SCRIPT type="text/javascript"> <% org.hibernate.Session ThisSession = SessionFactory.getSession(); org.hibernate.engine.SessionImplementor ThisSessionImplementor = (org.hibernate.engine

Control sort order of Hibernate EnumType.STRING properties

断了今生、忘了曾经 提交于 2019-12-05 08:14:35
Currently, my project uses @Enumerated(EnumType.ORDINAL) , so when I sort by this column, it is ordering based on the order in the enum , which works fine. But I need to add some additional values to the enum , which need to be inserted at different locations in the list of enum values and can't be just added to the bottom to maintain the correct sort order. If I do this, my database will be messed up. I'll have to write some scripts to translate all these ordinal values to the correct new ordinal. There is a possibility that more status will have to be added later. Since I have to fix all the

HQL select on embedded map

只谈情不闲聊 提交于 2019-12-05 08:01:59
I want to use (searchable) localization in my models, and i came up with the following model: @Entity public class Category { ... @ElementCollection //key = language code (e.g. 'en') and value = localized label private Map<String, String> name; ... } What I want to do no is to query for categories which contain an caseinsensitive needle in a particular localization (e.g. with '%abc%' in their english name) I tried something like from Category where name.key = :locale and lower(name.value) like :text but that fails with a cannot dereference scalar collection element exception. Now the hibernate

Simple hql named query that uses a inner join

て烟熏妆下的殇ゞ 提交于 2019-12-05 07:30:47
问题 I want to do something like this in my domain/entity object : @Entity @NamedQueries({ @NamedQuery(name="favouriteCats", query="from Cat c inner join on UserCat uc where uc.isFavourtie = true and uc.user = :user") }) public final class Cat extends BaseTable So that in my service layer I can do this : Query query = session.getNamedQuery("favouriteCats") query.setParameter(0, MyUser); return query.list(); However, my syntax in HQL is incorrect - and aftern ten minutes looking at official docs I

Hibernate polymorphic query

笑着哭i 提交于 2019-12-05 07:27:17
I have two classes, Person and Company, derived from another class Contact. They are represented as polymorphically in two tables (Person and Company). The simplified classes look like this: public abstract class Contact { Integer id; public abstract String getDisplayName(); } public class Person extends Contact { String firstName; String lastName; public String getDisplayName() { return firstName + " " + lastName; } } public class Company extends Contact { String name; public String getDisplayName() { return name; } } The problem is that I need to make a query finding all contacts with

Hibernate 3.6: registerFunction in SQL dialect not working

心不动则不痛 提交于 2019-12-05 06:37:41
I´m giving up and ask the community ... In my project, I´m using Hibernate 3.6.4.Final and a custom sql dialect: public class ServiceAppMySQL5InnoDBDialect extends MySQL5InnoDBDialect { public ServiceAppMySQL5InnoDBDialect() { super(); registerFunction("bitwise_and", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "(?1 & ?2)")); registerFunction("hasflags", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "?1 & ?2 = ?2")); } } Using the hasflags method in a HQL query fails. Here is the query: Query q = em .createQuery( "SELECT o FROM " + entityClass.getName() + " o WHERE hasflags(o

Join fetch: “query specified join fetching, but the owner of the fetched association was not present in the select list”

亡梦爱人 提交于 2019-12-05 06:22:54
I have a following code: public class ValueDAO implements BusinessObject<Long> { private Long id; private String code; private ClassDAO classDAO ; .... } public List<String> getCodesByCodeClass(Long classId) { String select = "select distinct val.code from ValueDAO val left " + "join fetch val.classDAO "; String where = "where val.classDAO.id = ? order by val.code"; return getHibernateTemplate().find(select + where, classId); } It raises an exception: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list In the