Get record with max id, using Hibernate Criteria

You can use a DetachedCriteria to express a subquery, something like this: DetachedCriteria maxId = DetachedCriteria.forClass(Foo.class) .setProjection( Projections.max(“id”) ); session.createCriteria(Foo.class) .add( Property.forName(“id”).eq(maxId) ) .list(); References Hibernate Core Reference Guide 15.8. Detached queries and subqueries

How to paginate a JPA Query

For all JPA query objects (except for native SQL queries), you would use pagination through the setMaxResults(int) and setFirstResult(int) methods. For instance: return em.createNamedQuery(“yourqueryname”, YourEntity.class) .setMaxResults(noOfRecords) .setFirstResult(pageIndex * noOfRecords) .getResultList(); JPA will perform the pagination for you. Named queries are just predefined and can be cached, while other types are dynamically created. So the choice … Read more

Hibernate query a foreign key field with ID

Hibernate “nicely wraps” only what you tell it to wrap. So, assuming that your Employee mapping looks something like: @Entity public class Employee { … @ManyToOne @JoinColumn(name=”address_id”) private Address address; … } and your Address has an id property, you can query based on address_id via: session.createCriteria(Employee.class) .add(Restrictions.eq(“address.id”, addressId)); In order to query based on … Read more

UNION to JPA Query

SQL supports UNION, but JPA 2.0 JPQL does not. Most unions can be done in terms of joins, but some can not, and some are more difficult to express using joins. EclipseLink supports UNION.

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

One easy way to accomplish this is combining what was posted in the original post into a single statement: int numberOfRecords = dtFoo.Select(“IsActive=”Y””).Length; Another way to accomplish this is using Linq methods: int numberOfRecords = dtFoo.AsEnumerable().Where(x => x[“IsActive”].ToString() == “Y”).ToList().Count; Note this requires including System.Linq.

How do you order a oneToMany join table in hibernate criteria

You need to add the @javax.persistence.OrderBy annotation to your kittens list. @OneToMany(fetch = FetchType.LAZY, targetEntity=Kittens.class, cascade=CascadeType.ALL) @JoinColumn(name=”motherId”) @OrderBy(“kittenName”) private List<Kittens> kittens; @See Hibernate Annotations Reference Guide Chapter 2.2.5. Mapping entity associations/relationships Subchapter 2.2.5.3.4. Indexed collections (List, Map)

How to achieve “not in” by using Restrictions and criteria in Hibernate?

Your question is somewhat unclear. Assuming “Category” is a root entity and “2,3” are ids (or values of some property of the category”) you can exclude them using the following: Criteria criteria = …; // obtain criteria from somewhere, like session.createCriteria() criteria.add( Restrictions.not( // replace “id” below with property name, depending on what you’re filtering … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)