hql

Many-To-One with join Table in hibernate resource classes for a JAX-RS using Jersey

∥☆過路亽.° 提交于 2019-12-01 05:28:44
I am implementing a RESTful Web Service using Jersey. I use hibernate to communicate with the database (mySQL). My hibernate resource classes includes: @Entity public class Activity { @Id @GeneratedValue private long id; @ManyToOne @JoinTable(name="category_activity", joinColumns={@JoinColumn(name="activities_id")}, inverseJoinColumns={@JoinColumn(name="Category_id")}) private Category category; } and the Category class: @Entity public class Category { @Id @GeneratedValue private long id; @OneToMany @Fetch(FetchMode.JOIN) @JoinTable(name = "category_activity", joinColumns = { @JoinColumn(name

Hibernate HQL查询

依然范特西╮ 提交于 2019-12-01 04:22:21
package cn.sasa.test; import org.hibernate.Session; import org.hibernate.Transaction; import cn.sasa.domain.account; import cn.sasa.utils.HibernateUtils; public class TestHQL { @org.junit.Test public void test() { Session session = HibernateUtils.getCurrentSession(); Transaction tran = session.beginTransaction(); String hql = "from account";//查询所有记录 var query = session.createQuery(hql); var list = query.list();//返回多个记录 //var list = query.uniqueResult();//返回一条记录 System.out.println(list); String hql1 = "from account where id=:id";//查找一条记录 var query1 = session.createQuery(hql1); query1

How to perform left join in Hibernate Query Language?

安稳与你 提交于 2019-12-01 04:08:18
This is my HQL query, but it isn't working and is throwing an error. Hql query: SELECT * FROM TABLEA A LEFT JOIN A.TABLEB B WHERE A.COLUMNNAME = B.COLUMNAME and it causes this error: org.hibernate.QueryException: This Query caught Exception. could not resolve property: of TABLEB:TABLEA. How can I solve this problem? Actually I retrieved a value from more than one table. This query doesn't work with CreateQuery(strQuery) . In HQL you can use LEFT JOIN only with linked property in main entity: Sample EntityA has an object entityB of type EntityB so you can SELECT A FROM EntityA A LEFT JOIN A

How to escape wildcard characters in “like” clause?

人走茶凉 提交于 2019-12-01 03:26:48
How can I escape the wildcard characters in a like clause? E.g.: select foo from Foo as foo where foo.bar like '%' || :filter ||'%' query.setParameter("filter", "%"); query.list(); // I'd expect to get the foo's containing the '%' in bar, not all of them! Any ideas? In Hibernate 3 you can use the escape parameter to specify the escape char: select foo from Foo as foo where foo.bar like '!%' escape '!' I think that should work, although I have never tried it in practice. 来源: https://stackoverflow.com/questions/3006524/how-to-escape-wildcard-characters-in-like-clause

Order By Rand by Time (HQL)

限于喜欢 提交于 2019-12-01 02:34:01
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 solution ? Seed the random number generator: order by rand(123) I would suggest using a session-scoped

Many-To-One with join Table in hibernate resource classes for a JAX-RS using Jersey

橙三吉。 提交于 2019-12-01 02:22:08
问题 I am implementing a RESTful Web Service using Jersey. I use hibernate to communicate with the database (mySQL). My hibernate resource classes includes: @Entity public class Activity { @Id @GeneratedValue private long id; @ManyToOne @JoinTable(name="category_activity", joinColumns={@JoinColumn(name="activities_id")}, inverseJoinColumns={@JoinColumn(name="Category_id")}) private Category category; } and the Category class: @Entity public class Category { @Id @GeneratedValue private long id;

Hibernate and Unexpected end of Subtree exception

隐身守侯 提交于 2019-12-01 02:13:15
I'm a newbie to Hibernate. I have an Item POJO which contains a Set<String> consisting of labels. The labels are contained on another Database table from the Item table, so I do a join to populate the pojo. I'm trying to run a simple example query from the book "Java Persistance with Hibernate" where I query from Item item where 'hello' member of item.labels . Only, for some reason I am getting a `org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree[from /*qualified class path*/.Item item where 'hello' member of item.labels]` What might be causing this issue? Here are my POJOs

How to perform left join in Hibernate Query Language?

荒凉一梦 提交于 2019-12-01 01:49:08
问题 This is my HQL query, but it isn't working and is throwing an error. Hql query: SELECT * FROM TABLEA A LEFT JOIN A.TABLEB B WHERE A.COLUMNNAME = B.COLUMNAME and it causes this error: org.hibernate.QueryException: This Query caught Exception. could not resolve property: of TABLEB:TABLEA. How can I solve this problem? Actually I retrieved a value from more than one table. This query doesn't work with CreateQuery(strQuery). 回答1: In HQL you can use LEFT JOIN only with linked property in main

HQL to get elements that possess all items in a set

梦想的初衷 提交于 2019-12-01 01:41:12
Currently, I have an HQL query that returns all Members who possess ANY Award from a set of specified Awards: from Member m left join m.awards as a where a.name in ("Trophy","Ribbon"); What I now need is HQL that will return all Members who possess ALL Awards specified in the set of Awards. So, assuming this data: Joe has Trophy, Medal Sue has Trophy, Ribbon Tom has Trophy, Ribbon, Medal The query above would return Joe, Sue, and Tom because all three possess at least one of Trophy or Ribbon. But I need to return only Sue and Tom, because they are the only ones who possess all of the specified

HQL IN operator, Array of Enums ClassCastException

流过昼夜 提交于 2019-12-01 01:17:01
Here is my stripped down class and enum. class A { @Enumerated (value = EnumType.STRING) AType type; } enum AType { X,Y } if I run query = FROM A a WHERE a.type = :type query.setParameter("type", AType.X); All is fine and dandy. However if I do the following: AType[] types = new AType[1]; types[0] = AType.X; query = FROM A a WHERE a.type IN (:types) query.setParameter("types", types); I get: Lcom.src.AType; cannot be cast to java.lang.Enum If I do: Enum[] types = new Enum[1]; types[0] = AType.X; query = FROM A a WHERE a.type IN (:types) query.setParameter("types", types); I get: Ljava.lang