hql

HQL error: with-clause referenced two different from-clause elements

大憨熊 提交于 2019-12-07 11:40:46
问题 I am getting started with Hibernate and I am using HQL, using some joins to retrieve data from the db but getting this error, any help on how to solve this is appreciated. Field.hbm.xml file: <id name="id" type="string"> <column name="field_map_cd" /> <generator class="assigned"></generator> </id> Rule.hbm.xml file: <many-to-one name="src_field_map" column="field_map_cd" not-null="true" insert="false" update="false"/> <many-to-one name="tgt_field_map" column="field_map_cd" not-null="true"

LIMIT in Postgres not supported in HQL?

余生颓废 提交于 2019-12-07 08:52:12
问题 I am having error using the following code in Grails executing HQL to get the first 30 Item objects: def items = Item.executeQuery('SELECT i FROM Item as i LIMIT 30 OFFSET 0') my backend database is Postgres. However, I got: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: LIMIT near line 1, column ... Obviously, the error tells that LIMIT is not supported by HQL. How do I make it work? In other words, how do I make HQL execute native SQL that is well supported by Postgres? 回答1:

Why does using a column name directly in HQL only work sometimes?

心已入冬 提交于 2019-12-07 06:14:02
问题 I have two HQL queries I am using for a quick-and-dirty unit test. The first looks somewhat like this: from Foo where SOME_FOREIGN_KEY = 42 The second looks like this: from Foo as foo inner join foo.Bar as bar where foo.SOME_FOREIGN_KEY = 42 The SOME_FOREIGN_KEY column is not the name of something that Hibernate knows is mapped. For some reason, the first HQL query works, but the second one does not. My goal here is to get the second version to work, without traversing the object graph to the

Hibernate HQL for joining non-mapped tables

风格不统一 提交于 2019-12-07 05:23:55
问题 I have an entity called "Kurs": @Entity public class Kurs { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long kursId; private String name; //Accessors.... } And also an entity called "Kategori": @Entity public class Kategori { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long kategoriId; private String name; @ManyToMany(cascade = CascadeType.ALL) @JoinTable (name = "KursKategori", joinColumns = {@JoinColumn(name = "kategoriId")}, inverseJoinColumns = {@JoinColumn

HQL: order by property of nullable property

最后都变了- 提交于 2019-12-07 04:48:31
问题 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? 回答1: What about : from A a left join a.b_fk b order by b.c The

Using HQL to query on a Map's Values

爱⌒轻易说出口 提交于 2019-12-07 03:16:27
Let's say I have a map (call it myClass.mapElem<Object, Object> ) like so: Key Val A X B Y C X I want to write HQL that can query the Values such that I can get back all instances of myClass where mapElem's value is 'X' (where 'X' is a fully populated object-- I just don't want to go through each element and say x.e1 = mapElem.e1 and x.e2=... etc). I know I can do this for the keys by using where ? in index(myClass.mapElem) , I just need the corresponding statement for querying the values! Thanks in advance... ETA: Not sure if the syntax makes a difference, but the way I am actually querying

Hibernate polymorphic query

拜拜、爱过 提交于 2019-12-07 03:04:14
问题 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

Hibernate entity only one column, no name

≯℡__Kan透↙ 提交于 2019-12-06 16:49:55
I want to map one column, without using a column name. I am using a count entity, and the want to use mulple different queries with the same entity : @Entity public class CountDTO extends Number { @Id // below causes an error in my test, hsql not same syntax @Column(name = 'COUNT') private Long count; In my prod (oracle) database I can just do select count() as COUNT from ... however, the same syntax doesn't work using hypersql in-memory db ? Is their an oracle/hsql compatible way to map a single column alias in HQL ? Your issue is that COUNT is a reserved keyword for HSQL , but not for Oracle

How to a write a Criteria query with multiple joins involved

早过忘川 提交于 2019-12-06 15:09:59
I'm trying to code the following HQL query using the Criteria API: var userList = _session .CreateQuery("select u from User u where u.Role.ID=3 and u.Customer.ID=:cID") .SetInt32("cID", 1) .List<User>(); (3 NHibernate objects : User(ID, Name, Role, Customer), Role(ID, Name) and Customer(ID, Name). I tried the following but it doesn't work because NHibernate tries to find a Customer associated with a Role: var userList = _session .CreateCriteria(typeof(User)) .CreateCriteria("Role") .Add(Restrictions.Eq("ID", 3) ) .CreateCriteria("Customer") .Add(Restrictions.Eq("ID", 1) ) .List<User>(); Any

HQL how to join three table

拟墨画扇 提交于 2019-12-06 14:32:22
I Have this classes: @Entity public class Category { private Long Id; private String name; private String description; private List<Product> products; } @Entity public class Inventory { private Long id; private Product product; private int quantity; } @Entity public class Product { private Long productId; private String name; } I want to get the Inventory given the id in the category. i'm trying to use this return session.createQuery("select i from Inventory i, Category c join c.Products p outer join i.product = p WHERE c.Id=?") .setParameter(0, categoryId).list(); I'm really confused, please