hibernate-criteria

Hibernate Criteria Transformers.aliasToBean is not populating correct values

拟墨画扇 提交于 2019-11-29 14:26:00
问题 I am trying to create BO by joining my entity classes Criteria criteria = session.createCriteria(Report.class,"r"); criteria .createAlias("template", "t") .createAlias("constituents", "rc") .createAlias("rc.entity", "pe") .createAlias("pe.model", "m") .createAlias("pe.scenario", "s") .setProjection(Projections.projectionList() .add( Projections.property("r.Id")) .add( Projections.property("t.Typ")) .add( Projections.property("pe.bId")) .add( Projections.property("m.model")) .add( Projections

Grails : how to best construct a hibernate criteria builder to search 'hasMany' relationships with domain instance

£可爱£侵袭症+ 提交于 2019-11-29 13:02:54
I am working on a grails project and would like to leverage hibernate criteria builders to search for instances of a domain object. I would like to find instances where one of the 'hasMany' relationships contains domain object with certain ids. Here is an example of what I mean. Domain Objects class Product { static hasMany = [ productOptions: ProductOption ] } class ProductOption{ Option option static belongsTo = [ product: Product ] } class Option{ String name } This is a simplified example of my domain structure and doesn't include all relationships. An Option could be size, color, brand,

Null list returned from hibernate query with embedded id

大兔子大兔子 提交于 2019-11-29 09:57:22
I have an entity with an embedded key. The entity has only the key as a field and the key has 7 fields, some of which can be null. When I run the following query: Criteria criteria = session.createCriteria(getPersistentClass()); criteria.add(Restrictions.eq("id.profPropertyId", profileExtensionName)); Object obj = criteria.list(); log.info(obj); return (List<ProfileExtensions>) obj; I get the correct number of results, but each result is null (i.e. I get a list of 4000 null objects). I have tried using both a HQL query and criteria but they both give the same result. The mapping classes were

How to implement with hibernate criteria Object the select query with inner join

♀尐吖头ヾ 提交于 2019-11-29 05:06:51
I'm new with Hibernate and Criteria Query. So I have implemented the query in HQL: select A.mobilephone B.userNick C.creditCard from mobile_table A inner join user_table B on A.codmobile=B.codmobile inner join Credit C on A.mobileCredit= C.mobileCredit How can I implement it with Hibernate Criteria Object? Your example is just a native SQL , not the HQL . Anyway , you can use the following methods from the Criteria API to construct the desired Criteria object : Use the setProjection(Projection projection) to define the select clause Use the createCriteria(String associationPath,String alias)

How to create specification using JpaSpecificationExecutor by combining tables?

﹥>﹥吖頭↗ 提交于 2019-11-29 04:36:40
I am using JpaSpecificationExecutor for creating custom queries. How do I create a Specification for the following SQL? select * from employee e, address a where e.id=23415 and e.name="Deepak" and a.city="Leeds"; Java Class : public static Specification<Employee> searchEmployee(final Map<String,String> myMap) { return new Specification<Employee>(){ @Override public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) { //Need to query two tables Employee and Address } } Here is a test that works @Test public void test1() { repository.save(makeEmployee("billy",

SQL 'LIKE' operator in Hibernate Criteria API

放肆的年华 提交于 2019-11-29 03:41:39
I want to implement some universal filter with Hibernate Criteria . It should work like LIKE operator from SQL: SELECT * FROM table WHERE table.ANYCOLOUMNHERE LIKE '%'||anyvaluehere||'%' I have Map<String, String> where key is a column name, and value is its value. I tried something like this: for (Entry<String, String> filter : filters.entrySet()) { crit.add(Restrictions.ilike(filter.getKey(), filter.getValue(), MatchMode.ANYWHERE)); } But when field type is not String , it causes java.lang.ClassCastException : [com.nsn.util.LoggerUtilerror] (http-localhost-127.0.0.1-8080-1) Error while

How do you select a column using Hibernate?

最后都变了- 提交于 2019-11-29 00:24:04
问题 I would like to select a single column instead of a whole object, using Hibernate. So far I have this: List<String> firstname = null; firstname = getSession().createCriteria(People.class).list(); My problem is that the above code returns the whole People table as an object instead of just "firstname". I'm not sure how to specify to only return "firstname" instead of the whole object. 回答1: You can set the Projection for this like: .setProjection(Projections.property("firstname")) With this you

Java - Hibernate criteria.setResultTransformer() initializes model fields with default values

北城余情 提交于 2019-11-28 23:30:46
I am new to Hibernate and I am trying to get some data from the database. I don't want to get the full data but a projection of an entity. The thing is that in the for-loop when I get the id and the name of my projection, it gets the default values id=0 and name=null instead of id=7 and name="Name 8" which are the records of the original entity in the database. Do you know what causes this problem? The for-loop is in the last code. Here is the Student Entity @Entity(name = "Students") public class Student { @Id @GeneratedValue @Column(name = "StudentId") private int id; @Column(name = "Name",

Hibernate criteria on collection values

心不动则不痛 提交于 2019-11-28 20:47:33
I'm trying to put together a complicated query using Hibernate. I've been leaning toward Criteria, but I'm beginning to suspect it's not possible, and so any suggestions would be helpful. I have an entity structure like the following: public class Attribute { private Integer id; private String name; private Set<Value> values; } public class Instance { private Integer id; private int instanceRef; private Set<Value> values; } public class Value { private Integer id; private Attribute attribute; private String localAttributeName; private Instance instance; private String value; } These entities

JPA2: Case-insensitive like matching anywhere

丶灬走出姿态 提交于 2019-11-28 17:45:52
I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike("column","keyword", MatchMode.ANYWHERE) which tests if the keyword matching the column anywhere and it is case-insensitive. Now, I am using JPA 2.0 with EclipseLink as driver so I have to use "Restrictions" build-in JPA 2.0. I found CriteriaBuilder and method like , I have also found out how to make it matching anywhere ( although it is aweful and manual ), but still I haven't figured out how to do it case-insensitive. There is my current aweful solution: CriteriaBuilder builder = em