criteria

Finding out the page containing a given record using JPA (Hibernate)

拜拜、爱过 提交于 2019-12-05 03:35:29
How can I know the position of a record in a JPA query? I have a service that returns paged results implementing more or less a method with this signature: List<Record> getRecordsPage(long page, int pageSize); When this is invoked I just create a query and configure like this: TypedQuery<Record> query = entityManager.createQuery(criteriaQuery); query.setFirstResult(page * pageSize); query.setMaxResults(pageSize); This pages the result. And this is working as expected, and is quite simple. The Problem Another requirement I have is to implement a method that would retrieve the page that contains

Getting “with clause can only reference columns in the driving table” when trying to write a JPA 2.1 left outer join

亡梦爱人 提交于 2019-12-05 03:33:52
I’m using JPA 2.1 and Hibernate 4.3.6.Final. I’m trying to use CriteriaBuilder to write a left outer join with conditions, so I have final CriteriaBuilder cb = m_entityManager.getCriteriaBuilder(); CriteriaQuery<Message> query = cb.createQuery(Message.class); Root<Message> messageRoot = query.from(Message.class); final Join<Message, Group> groupJoin = messageRoot.join(Message_.group); final Join<Message, MessageReadDate> msgReadDateJoin = messageRoot.join(Message_.messageReads, JoinType.LEFT); // form left outer join clause. msgReadDateJoin.on( cb.equal(messageRoot, msgReadDateJoin.get

how to do this with NHibernate criteria

蓝咒 提交于 2019-12-05 02:30:50
问题 let's say I have 2 tables table1(a,b) and table2(c,a) I need to do something like this, but with NHibernate criteria: select a,b, (select count(*) from table2 t2 where t1.a = t2.a ) x from table1 t1 anybody knows how to do this ? 回答1: with Projections.SubQuery var l = session.CreateCriteria<Table1>("t1") .SetProjection(Projections.ProjectionList() .Add(Projections.Property("a"), "a") .Add(Projections.Property("b"), "b") .Add(Projections.SubQuery( DetachedCriteria.For<Table2>("t2")

Does HQL query always hit database and get results?

被刻印的时光 ゝ 提交于 2019-12-04 22:18:04
问题 I was going through hibernate and situations when to use Criteria vs HQL and my understanding is that with Hibernate, everytime when we are querying database either by Criteria or HQL in both instances hibernate would get result set and put in memory and then when we call that query again, data would be fetched from memory rather then hitting that database, is my understanding correct? Also as you can see from comments to question mentioned below, it was suggested that Hibernate Criteria

Can I call a stored procedure with hibernate criteria?

冷暖自知 提交于 2019-12-04 22:13:13
问题 Can I use Hibernate criteria to call a stored procudure? 回答1: See Using stored procedures for querying in the reference documentation. Mapped queries are called like this. List employment = sess.getNamedQuery("BigSP") .list(); A mapped query can return entities. <sql-query name="BigSP" callable="true"> <return alias="emp" class="Employment"> <return-property name="employee" column="EMPLOYEE"/> <return-property name="employer" column="EMPLOYER"/> <return-property name="startDate" column=

NHibernate Lambda Extensions - Eager Loading a collection's assosciations

青春壹個敷衍的年華 提交于 2019-12-04 21:06:17
I've got a Criteria Query for a social networking site. A Person object has a collection of Friends (also person objects). The query grabs the first N friends, but I also want to eager load an associated object MainProfileImage and then a subsequent associated object MediumThumbnail. I can do this in HQL easily: select friends from Person person inner join person.Friends friends inner join fetch friends.MainProfileImage image inner join fetch image.MediumThumbnail where person = :person1 order by friends.LatestLogin desc Here's My Criteria effort. For some reason this doesn't return anything!

How to create criteria in groovy/grails for nested object?

耗尽温柔 提交于 2019-12-04 17:56:45
问题 I need help on creating hibernate criteria for nested object. For example : class office{ Integer id; OfficeDetails cmdData ; } class OfficeDetails { Integer id; Region region; } class Region { Integer id; Integer regionNum; } Now, from the service class ( officeService) I am trying to pull up all of the offices that matches a certain region as : List<Office> findAllByRegion( Integer regionNumber){ def criteria = { eq ( 'cmdData.region.regionNum', regionNumber ) } def allOfficesInTheRegion =

ElementCollection createAlias in hibernate API

六眼飞鱼酱① 提交于 2019-12-04 17:54:25
问题 does anyone know if and how the solution for following question (which is written in the JPA API) can be written using the hibernate criteria API? To be more specific I have a Discussion entity that contains a list of participants (which is a list of usernames): @ElementCollection @Column(name = "user_name") @CollectionTable(name = "DISCUSSION_USER", joinColumns = @JoinColumn(name = "DISCUSSION_ID")) @OrderColumn(name = "ORDER_INDEX") private List<String> participants = new ArrayList<String>(

Hibernate criteria projection distinct

自闭症网瘾萝莉.ら 提交于 2019-12-04 17:04:14
问题 Hi i want to write a query using criteria : The following query has to be created using criteria: "Select Distinct(s2Taxper) from S2 where s2Tc='601' AND s2Txcd!=''" thanks in advance 回答1: Criteria criteria = session.createCriteria(S2.class) .add(Restrictions.eq("s2Tc","601")) .add(Restrictions.ne("s2Txcd","")) .setProjection(Projections.distinct(Projections.property("s2Taxper"))); 来源: https://stackoverflow.com/questions/12032748/hibernate-criteria-projection-distinct

Retrieving Polymorphic Hibernate Objects Using a Criteria Query

五迷三道 提交于 2019-12-04 16:37:29
问题 In my model I have an abstract "User" class, and multiple subclasses such as Applicant, HiringManager, and Interviewer. They are in a single table, and I have a single DAO to manage them all. User: @Entity @Table(name="User") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="role", discriminatorType=DiscriminatorType.STRING ) public abstract class User extends BaseObject implements Identifiable<Long> ... HiringManager (for example): @Entity @DiscriminatorValue(