hql

HQL with a collection in the WHERE clause

你离开我真会死。 提交于 2019-12-04 03:52:23
I've been trying for the this whole a query who is officially giving me nightmares. The system is a user and contact management. So I have UserAccount , Contact and Phone . UserAccount has a bidirectional one-to-many relationship with Contact and an unidirectional one on phone all mapped by a Set : //UserAccount mapping @OneToMany(targetEntity=PhoneImpl.class, cascade= {CascadeType.ALL}) @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN) private Set<Phone> phones = new HashSet<Phone>(); @OneToMany(targetEntity=ContactImpl.class, cascade={CascadeType

Hibernate的批量查询——HQL

╄→гoц情女王★ 提交于 2019-12-04 03:37:45
HQL(Hibernate Query Language)查询: 1、查询所有学生信息: public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); String hql="from pers.zhb.domain.Student"; Query query= session.createQuery(hql); List<Student>list=query.list(); System.out.println(list); transaction.commit(); session.close();//游离状态 } 2、条件查询(查询所有的女学生的信息): public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); String hql="from pers.zhb.domain.Student where sex='女'"; Query query= session

Hql, How to write join query between tables that has one to many relationship?

醉酒当歌 提交于 2019-12-04 03:30:06
问题 I have 2 tables. 1st one have oneToMany relationship with 2nd . Class Author @Entity @Table(name = "Author") Public class Author{ @Id @Column(name = "AuthorId") private int autherId; @Column(name = "AuthorName") private String authorName; @OneToMany @JoinColumn(name="AuthorId",referencedColumnName="AuthorId") List<Book> Books; //getter and setter } Class Book @Entity @Table(name = "Book") Public class Book{ @Id @Column(name = "BookId") private int bookId; @Column(name = "BookName") private

How to set limit for a hibernate query

房东的猫 提交于 2019-12-04 03:26:04
How can I set a limit to this hql query? When I add the limit keyword in the query, an error is thrown. @Query("from voucher v where v.voucherType.typeDescription = :typeDescription and v.denomination = :denomination") public List<Voucher> findByVoucherTypeAndDenomination(@Param("typeDescription") String typeDescription,@Param("denomination") BigDecimal denomination); When you call your query add the following: .setFirstResult(firstResult).setMaxResults(limit); UPDATE Docs: http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Query.html#setMaxResults(int) If you use entityManager, it

Hibernate hql, execute multiple update statements in same query

北战南征 提交于 2019-12-04 02:57:18
I want to execute multiple update statements in the same query in hibernate Hql. like below: hql = " update Table1 set prob1=null where id=:id1; " + " delete from Table2 where id =:id2 "; ... query.executeUpdate(); in the same executeUpdate call I want to update records in Table1 and delete records from Table2. Is that possible? Adrian Shum In short, what you are looking is something like batching in JDBC. Thich is not provided by Hibernate for Bulk Update query, and I doubt if it will ever be considered for Hibernate. From my past experience, Batching feature for HQL is rarely useful in real

Hibernate: how to make EXISTS query? (not subquery)

▼魔方 西西 提交于 2019-12-04 02:43:31
For example: EXISTS ( SELECT * FROM [table] WHERE ... ) How to make such query using Hibernate? If your goal is inspect some set on emptiness, you may use simple HQL query: boolean exists = (Long) session.createQuery("select count(*) from PersistentEntity where ...").uniqueResult() > 0 HQL doesn't allow to use exists statement. UPD Starting from Hibernate 5 it supports it as a predicate in WHERE You can use several approaches: For Hibrnate 5: you can use subquery with the same table boolean exists = session.createQuery("select 1 from PersistentEntity where exists (select 1 from

How to order by count desc in each group in a hive?

心已入冬 提交于 2019-12-04 02:34:29
Here's the HQL: select A, B, count(*) as cnt from test_table group by A, B order by cnt desc; The sample output is as follows: a1 | b1 | 5 a2 | b1 | 3 a1 | b2 | 2 a2 | b2 | 1 But what I want is to do the order by in each group of A, and the intended output is like: a1 | b1 | 5 a1 | b2 | 2 a2 | b1 | 3 a2 | b2 | 1 Could anyone can give me some idea how to resolve this problem in just one HQL? Thanks a lot! select A, B, count(*) as cnt from test_table group by A, B order by A, cnt desc; Try this query: If you want only order of A then: select A, B, count(*) as cnt from test_table group by A, B

What's the best way to write if/else if/else if/else in HIVE?

时间秒杀一切 提交于 2019-12-04 00:26:24
Hive uses IF(condition, expression, expression), so when I want to do if / else if / else if / else, I have to do: IF(a, 1, IF(b, 2, IF(c, 3, 4))) Is there a better way to do this that's more readable? Looking for something similar to the standard if (a) { 1 } else if (b) { 2 } else if (c) { 3 } else { 4 } You can use Hive Conditional CASE WHEN function for if-else scenario. The CASE Statement will provide you better readability with the same functionality. CASE WHEN (condition1) THEN result1 WHEN (condition2) THEN result2 WHEN (condition3) THEN result3 WHEN (condition4) THEN result4 ELSE

HQL: Hibernate query with ManyToMany

元气小坏坏 提交于 2019-12-04 00:05:28
I have a question with HQL query and hibernate. I have a user class and a role class. A user can have many roles. So I have a ManyToMany relatation like this: In user class: @ManyToMany(fetch = FetchType.LAZY) @oinTable(name = "PORTAIL_USERROLE", joinColumns = { @JoinColumn(name = "USERID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "ROLE", nullable = false, updatable = false) }) public Set<Portailrole> getPortailroles() { return this.portailroles; } In role class: @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "PORTAIL_USERROLE", joinColumns = {

Update several Columns in one Hibernate Query?

江枫思渺然 提交于 2019-12-03 23:35:14
i have the Following HQL: String hql = "UPDATE Buchung as b " + "set STORNO = :Storno " + "where ID = :BuchungID"; Is it possible to Update more then one column in a HQL? For Example: String hql = "UPDATE Buchung as b " + "set STORNO = :Storno " + "set NAME = :Name " + ...... "where ID = :BuchungID"; I know how to do that in MSSQL but i dont know how to do that in Hibernate. HQL is no different than SQL in this case. Just use comma to separate columns: String hql = "UPDATE Buchung as b set " + "STORNO = :Storno," + "NAME = :Name " + ...... "where ID = :BuchungID"; The syntax is similar to the