criteria

Hibernate one-to-many search with Criteria

亡梦爱人 提交于 2019-12-07 11:41:40
问题 I've got a Hibernate entity, called Event, which has a one-to-many metadata entity, EventData. Given the following Event: EventId: 1 EventHash: broccoli With the following EventDatas: EventDataId: 1 EventId:1 Field: tag Content: tagme EventDataId: 2 EventId: 1 Field: tag Content: anotherTag How do I create a Criteria query to retrieve the event which has BOTH tags "anotherTag" and "tagme"? In SQL, I'd join the event_data table once for each tag being searched for, but I can only seem to

Hibernate query with an alias

雨燕双飞 提交于 2019-12-07 05:04:20
问题 What's wrong with session.createCriteria(Composed.class, "main") .createAlias("main.id.branch", "b1") .add(Restrictions.eq("b1.owner", user)) .list(); ? The corresponding HQL works fine String hql = "select main from Composed as main" + "left join main.id.branch as b1 where b1.owner = ?"; session.createQuery(hql) .setInteger(0, user.id().intValue()) .list(); From the Criteria, Hibernate creates no join and uses where b1x1_.owner_id=? , but there's no b1x1_ anywhere, so it fails with "could

How does hibernate use an empty string for an equality restriction?

喜你入骨 提交于 2019-12-07 03:30:33
问题 I have a column that potentially has some bad data and I can't clean it up, so I need to check for either null or empty string. I'm doing a Hibernate Criteria query so I've got the following that returns incorrectly right now: Session session = getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Criteria myCriteria = session.createCriteria(Object); ... myCriteria.add(Restrictions.or(Restrictions.isNull("stringColumn"), Restrictions.eq("stringColumn", ""))); List

Hibernate Criteria for elements inside a Set

£可爱£侵袭症+ 提交于 2019-12-07 03:01:03
问题 I have one entity that contains a set of another entity. Entity1 contains Set entityTwos I want to create a search criteria for an "id" field inside entityTwos. I searched, but didn't get any answers. Anybody have an idea? Thanks, Sri 回答1: It was easy. Something like: criteria.createAlias("entityTwos", "entityTwo"); criteria.add(Restrictions.eq("entityTwo.id", ...)); 来源: https://stackoverflow.com/questions/2941962/hibernate-criteria-for-elements-inside-a-set

Yii CDbCriteria Join

狂风中的少年 提交于 2019-12-07 02:53:05
问题 How I can write the query SELECT * FROM doc_docs dd JOIN doc_access da ON dd.id=da.doc_id AND da.user_id=7 with CDbCriteria syntax? 回答1: You actually cant completely write that since you have to apply the criteria to an activerecord model to obtain the primary table, but assuming you have a DocDocs model you can do it like this: $oDBC = new CDbCriteria(); $oDBC->join = 'LEFT JOIN doc_access a ON t.id = a.doc_id and a.user_id = 7'; $aRecords = DocDocs::model()->findAll($oDBC); Although it

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

爱⌒轻易说出口 提交于 2019-12-06 23:23:44
问题 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

NHibernate Lambda Extensions - Eager Loading a collection's assosciations

三世轮回 提交于 2019-12-06 16:57:02
问题 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

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

NHibernate: Creating a criteria which applies for all queries on a table

百般思念 提交于 2019-12-06 13:32:45
Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion on all queries on a table? For example, a good amount of of my tables have a "UserId" column. I might want to ensure that I am always selecting rows for the logged in user. I can easily create an ICriterion object, but I am forced to supply it for different methods: FindAll(), FindFirst(), FindLast() etc. Is there a way to force a WHERE clause on all queries to a Castle ActiveRecord? I finally found a great solution (using filters). Since Castle AR does not have any native API for mapping to NHibernate filters,

Doing Join query using CDBCriteria

强颜欢笑 提交于 2019-12-06 11:15:26
I am trying to do a Join query using CDBCriteria in Yii framework. The issue is the join query works successfully but it does not display the columns from other tables. I am doing in the following way $criteria = new CDbCriteria; $criteria->order = 't.id desc'; $criteria->select = '*'; $criteria->join = ' INNER JOIN table2 INNER JOIN table3 INNER JOIN table4'; When i run this, I can see only the mail table1 columns displayed. Other columns are not shown. In my model class, I have the relation has public function relations() { // NOTE: you may need to adjust the relation name and the related //