criteria

Retrieving Polymorphic Hibernate Objects Using a Criteria Query

冷暖自知 提交于 2019-12-03 10:44:23
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("HIRING_MANAGER") public class HiringManager extends User ... Now if I wanted to, say, get all the hiring

ElementCollection createAlias in hibernate API

旧城冷巷雨未停 提交于 2019-12-03 10:41:54
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>(); Now I need to retrieve all Discussions where a given username is a participant. If I would have

Cannot be cast to java.io.Serializable

偶尔善良 提交于 2019-12-03 10:11:45
I am currently using criteria to retrieve the details of a user, but when trying to query the details object with the right user, I get a ClassCastException. My Criteria Code; Criteria criteria = sess.createCriteria(UserDetails.class) criteria.add(Restrictions.eq("user.id", user.id)); I also tried using; Criteria criteria = sess.createCriteria(UserDetails.class) Criteria subCriteria = criteria.createCriteria("user"); subCriteria.add(Restrictions.eq("id", user.id)); Both give me the ClassCastException. I know I can easily solve it by letting the User implement Serializable, but is there any

Grails/GORM “in” criteria

倾然丶 夕夏残阳落幕 提交于 2019-12-03 10:03:34
Is it possible to do an "in" criteria using the GORM criteria. I'm looking for the equivalent of the following SQL select * from Person where age in (20,21,22); If it was possible I guess the syntax would be something like: def results = Person.withCriteria { in "age", [20, 21, 22] } The Grails createCriteria documentation includes an example of using the in clause: 'in'("holderAge",[18..65]) or not{'in'("holderAge",[18..65])} The documentation includes this note: Note: 'in' is a groovy reserve word, so it must be escaped by quotes. Yep, you have it almost exactly right. Just change in to 'in'

SQL Server query by column pair

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm working on products filter (faceted search) like Amazon. I have a table with properties (color, ram, screen) like this: ArticleID PropertyID Value --------- ---------- ------------ 1 1 Black 1 2 8 GB 1 3 15" 2 1 White 2 2 8 GB 3 3 13" I have to select articles depending on what properties are selected. You can select multiple values for one property (for example RAM: 4 GB and 8 GB) and you can select multiple properties (for example RAM and screen size). I need functionality like this: SELECT ArticleID FROM ArticlesProperties WHERE

rails mongoid clear criteria

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Mongoid::Paranoia adds a default scope to the model which generates the criteria #<Mongoid::Criteria selector : {: deleted_at =>{ "$exists" => false }}, options : {}, class : Line , embedded : false > I can find deleted documents with Model.deleted which generates, #<Mongoid::Criteria selector : {: deleted_at =>{ "$exists" => true }}, options : {}, class : Line , embedded : false > How can i override this so i can search both deleted and non-deleted documents. PS Model.unscoped does not work 回答1: Try this(its kind of hack): class

hibernate criteria with exists clause

可紊 提交于 2019-12-03 08:40:06
问题 I cannot find a solution to a problem that seems to be easy. Say there are 2 entity classes: class A { Set<B> bs; } class B { String text; } How to create a criteria query that returns all A's that contains at least one B entity which fulfills a given condition (like b.text = 'condition')? 回答1: I think this link can be useful: http://mikedesjardins.net/2008/09/22/hibernate-criteria-subqueries-exists/ It contains the following example about how create n exists criteria: "What you’re really

Spring Specification - conjunction of Predicates

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need a function that will be filter parameters and build query. I have 4 parameters therefore if I would try to implement query for each condition I would have to write 16 (2^4) implementations - it's not good idea. I try to improve my code with interface Specification from Spring Data JPA but I cannot create conjunction of predicates. Implementation of Specification interface : public class UserSpecification implements Specification { private final UserSearchCriteria criteria; public UserSpecification(UserSearchCriteria criteria) { this

Hibernate Criteria n+1 issue with maxresults

梦想与她 提交于 2019-12-03 06:44:56
Using hibernate ctiteria I want to select an object and it's associated oneToMany list of objects. I want to paginate through this list avoiding the dreaded hibernate n+1 select issue Here's a working solution which requires 11 trips to the database for 10 parent objects. Criteria criteria = this.getSession().createCriteria(Mother.class); criteria.addOrder(Order.asc("title")) .setMaxResults(details.getMaxRows()) .setFirstResult(details.getStartResult()) .setFetchMode("kittens", FetchMode.SELECT); List test = criteria.list(); And here's a solution which executes only one sql statement (hurray)

Getting a count of rows in a datatable that meet certain criteria

北城余情 提交于 2019-12-03 05:28:24
问题 I have a datatable, dtFoo, and would like to get a count of the rows that meet a certain criteria. EDIT: This data is not stored in a database, so using SQL is not an option. In the past, I've used the following two methods to accomplish this: Method 1 int numberOfRecords = 0; DataRow[] rows; rows = dtFoo.Select("IsActive = 'Y'"); numberOfRecords = rows.Length; Console.WriteLine("Count: " + numberOfRecords.ToString()); Method 2 int numberOfRecords = 0; foreach (DataRow row in dtFoo.Rows) { if