criteria

Hibernate how to use order by clause for sort by sum of field

喜你入骨 提交于 2019-12-10 21:38:47
问题 How to use order by clause for sort by sum of field Entity: class MyClass { private int a; private int b; //... } HQL works correctly: "SELECT myclass FROM MyClass myclass ORDER BY myclass.a + myclass.b DESC " How to do the same thing using Criteria API? 回答1: As you said you could use @Formula @Column(name="column_a") private Integer a; @Column(name="column_b") private Integer b; @Formula("(column_a+column_b)") private Integer c; After that you can sort by property c like criteria.addOrder

ResultTransformer in Hibernate return null

情到浓时终转凉″ 提交于 2019-12-10 20:48:51
问题 I'm using ResultTransformer to select only particular properties from entity, just i don't need all properties from entity. But the problem i faced is when a property is "one-to-many". Here is a simple example. @Entity @Table(name = "STUDENT") public class Student { private long studentId; private String studentName; private List<Phone> studentPhoneNumbers = new ArrayList<Phone>(); @Id @GeneratedValue @Column(name = "STUDENT_ID") public long getStudentId() { return this.studentId; }

How to find the indices of an R list meeting multiple criteria

旧城冷巷雨未停 提交于 2019-12-10 17:19:33
问题 Suppose I have the following data frame in R: set.seed(5) PosActions <- c("Work","Pause","Clockin","Clockout","Lunch") df <- data.frame(ID = c(rep(1,3),rep(2:3,each=4),rep(4,5)), ACTION = sample(PosActions,16,replace=T)) Which returns ID ACTION 1 1 Pause 2 1 Clockout 3 1 Lunch 4 2 Pause 5 2 Work 6 2 Clockout 7 2 Clockin 8 3 Lunch 9 3 Lunch 10 3 Work 11 3 Pause 12 4 Clockin 13 4 Pause 14 4 Clockin 15 4 Pause 16 4 Pause In this data frame, the rows corresponding to ID == 2 and ID == 3 (rows 4

How to query a subproperty with NHibernate’s criteria api and the entity to load only the subproperties matching a predicate condition

ⅰ亾dé卋堺 提交于 2019-12-10 17:06:56
问题 Assuming the following: public class Order { public virtual int OrderId {get;set} public virtual ISet<Product> Products {get;set} } public class Product { public virtual int ProductId {get;set} public virtual string ProductName {get;set} } How would you query using the criteria api so that only an order with a specific orderid is returned and its Product collection should also be filtered down to Products whose Name start with the lettter P? 回答1: I would go about this with a DetachedCriteria:

Hibernate Criteria: find entity if any of its children's children have a specific property

孤街浪徒 提交于 2019-12-10 15:44:01
问题 I need to write a Criteria(or hql) to find a parent entity by a property of a child of its children entities. Here is my entities: // The top level parent class public class A { private Long id; private String someProperty; private B b; // and some other attributes... } // The second level parent class :) public class B { private Long id; private List<C> cList; // and some other attributes... } public class C { private Long id; private B b; private List<D> dList; // Other attributes.. }

Projections.countDistinct with Hibernate produces unexpected result

北慕城南 提交于 2019-12-10 14:23:21
问题 I have the following code Criteria criteria = this.getCriteriaForClass(DeviceListItem.class); Projection rowCountProjection = Projections.countDistinct("color"); criteria.setProjection(rowCountProjection); int rowCount = ((Long) criteria.uniqueResult()).intValue(); return rowCount; , whose purpose is to find out the number of rows with different values for the field named "color". The problem is that Projections.countDistinct("color"); returns the same number of results as Projections.count(

Hibernate 4.1 Count Projection Type Mismatch (Long/Integer) using Result Transformer

廉价感情. 提交于 2019-12-10 14:13:17
问题 I have an entity bean FooEntity and DAO method to get row counts grouped by a property on that entity, encapsulated in a view model bean FooCount . public List<FooCount> groupByFoo() { return sessionFactory.getCurrentSession() .createCriteria(FooEntity.class) .setProjection(Projections.projectionList() .add(Projections.groupProperty("foo"), "foo") .add(Projections.count("foo"), "count") ).setResultTransformer(Transformers.aliasToBean(FooCount.class)) .list(); } public class FooCount { private

“IllegalArgumentException occurred calling getter of” while running criteria with SINGLE_TABLE Inheritance strategy

两盒软妹~` 提交于 2019-12-10 13:22:42
问题 I have this error while trying to list objects from database with hibernate Criteria decorated with simple Restrictions Criteria criteria = session.createCriteria(Licence.class); criteria.add(Restrictions.eq("gym", gym.getId())); List<Licence> list = criteria.list(); I have two classes: Licence which has an association to Gym . Those two classes are extending DataModel which is for managing information about data edition - (creation and etition, who and when). What is also important that

Retrieve emebedded or component using Hibernate Criteria api

一笑奈何 提交于 2019-12-10 13:09:38
问题 I have this class mapped as a entity, lets call it Person. Person has an embedded/component relation to Address. I am having trouble using a Criteria that would return Address objects. I have tried this: Criteria.createCriteria(Address.class) Which does not work. I guess I need to go through the entity but then I would need some kind of projection? Criteria.createCriteria(Person.class).<<what goes here???>> Suggestions? 回答1: Component's lifetime is controlled by its owner; they are NOT

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

微笑、不失礼 提交于 2019-12-10 11:28:46
问题 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? 回答1: I finally found a