criteria

Hibernate: ordering a Set

泄露秘密 提交于 2019-11-29 12:10:09
问题 I have a class Person who has a set of Books. It is not meaningful in the particular case to have an ordered or sorted collection. Say now that I have a search page with a table showing the join of Person and Book. I want to be able to sort the results by fields from both Person AND Book, and then get a List from Hibernate, and iterate over it. Because the collection is a Set, the ordering of the Books has vanished (PersistentSet of Hibernate wraps a HashSet of Books, which is not ordered).

Using the 'case…when…then…else…end' construct in the 'having' clause in JPA criteria query

别来无恙 提交于 2019-11-29 11:51:11
The following criteria query calculates the average of rating of different groups of products. CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple>criteriaQuery=criteriaBuilder.createQuery(Tuple.class); Metamodel metamodel=entityManager.getMetamodel(); EntityType<Product>entityType=metamodel.entity(Product.class); Root<Product>root=criteriaQuery.from(entityType); SetJoin<Product, Rating> join = root.join(Product_.ratingSet, JoinType.LEFT); Expression<Number> quotExpression = criteriaBuilder.quot(criteriaBuilder.sum(join.get(Rating_.ratingNum)),

NHibernate Left Outer Join

让人想犯罪 __ 提交于 2019-11-29 10:57:12
I'm looking to create a Left outer join Nhibernate query with multiple on statements akin to this: SELECT * FROM [Database].[dbo].[Posts] p LEFT JOIN [Database].[dbo].[PostInteractions] i ON p.PostId = i.PostID_TargetPost And i.UserID_ActingUser = 202 I've been fooling around with the critera and aliases, but I haven't had any luck figuring out how do to this. Any suggestions? I actually figured it out. You need to do a check for null .CreateCriteria("Interactions", "i", NHibernate.SqlCommand.JoinType.LeftOuterJoin) .Add(Expression.Or(Expression.Eq("i.ActingUser", user), Expression.IsNull("i

How to query on a month for a date with Hibernate criteria

﹥>﹥吖頭↗ 提交于 2019-11-29 07:52:24
I need to use criteria to query a database. The data I'm searching has a date, say 'startDate' and I have a month say 0 for January, I need to extract all data where startDate has month = 0; in sql I'd use something like 'where month(startDate) = 0' but I don't know how to do this with hibernate criteria and if it's possible at all. Can you help me? Thank you guys. Luca. With criteria, I think you'll have to write your own expression class. Something like this should work (not tested, though): public class MonthEqExpression implements Criterion { private final String propertyName; private

Complex Hibernate Projections

邮差的信 提交于 2019-11-29 07:23:40
I want to ask, it is possible that I create query projections and criterion for more than one level deep? I have 2 model classes: @Entity @Table(name = "person") public class Person implements Serializable { @Id @GeneratedValue private int personID; private double valueDouble; private int valueInt; private String name; @OneToOne(cascade = {CascadeType.ALL}, orphanRemoval = true) @JoinColumn(name="wifeId") private Wife wife; /* * Setter Getter */ } @Entity @Table(name = "wife") public class Wife implements Serializable { @Id @GeneratedValue @Column(name="wifeId") private int id; @Column(name=

Android开发进阶(三)--初探Android平台上的定位服务(GPS)

怎甘沉沦 提交于 2019-11-29 06:39:43
初探Android平台上的定位服务(GPS) 特别声明 :以下所有操作都别忘记了加上权限: [html] view plain copy print ? < uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name = "android.permission.INTERNET" /> < uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" /> sp; LocationManager 通过LocationManager可以实现设备的定位、跟踪和趋近提示。它不需要你直接来实例化,我们可以通过Context.getSystemService(Context.LOCATION_SERVICE). 来获得LocationManager实例。 常用属性和方法 属性和方法 描述 GPS_PROVIDER 静态字符串常量,表明LocationProvider是GPS NETWORK_PROVIDER 静态字符串常量,表明LocationProvider是网络 addGpsStatusListener(GpsStatus.Listener

How to use hibernate criteria to return only one element of an object instead the entire object?

一世执手 提交于 2019-11-29 04:46:07
问题 I'm trying to get only the list of id of object bob for example instead of the list of bob. It's ok with a HQL request, but I would know if it's possible using criteria ? An example : final StringBuilder hql = new StringBuilder(); hql.append( "select bob.id from " ) .append( bob.class.getName() ).append( " bob " ) .append( "where bob.id > 10"); final Query query = session.createQuery( hql.toString() ); return query.list(); 回答1: I think you could do that with Projections, something like

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 I write a MAX query with a where clause in JPA 2.0?

岁酱吖の 提交于 2019-11-29 03:28:20
I'm using JPA 2.0. Hibernate 4.1.0.Final, and Java 6. How do I write a JPA query from the following psuedo-SQL? select max(e.dateProcessed) from Event e where e.org = myOrg And my domain object looks like the following: @GenericGenerator(name = "uuid-strategy", strategy = "org.mainco.subco.core.util.subcoUUIDGenerator") @Entity @Table(name = "sb__event", uniqueConstraints = { @UniqueConstraint(columnNames={"EVENT_ID"}) } ) public class Event { @Id @Column(name = "ID") @GeneratedValue(generator = "uuid-strategy") private String id; @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType

Hibernate Criteria API - adding a criterion: string should be in collection

谁说胖子不能爱 提交于 2019-11-29 02:26:52
I have to following entity object @Entity public class Foobar { ... private List<String> uuids; ... } Now I'd like to make a criteria query which would fetch all Foobar pojos whose uuids list contains the string "abc123", I'm just not sure how to make the appropriate criterion. I assume you are using a version of Hibernate that implements JPA 2.0. Here's a JPA 2.0 solution that should work with any compliant implementation. Please annotate uuids with JPA's @ElementCollection annotation. Don't use Hibernate's @CollectionOfElements as mentioned in some of the other answer comments. The latter