How to query data for Primefaces dataTable using lazy loading and pagination

In case of very large resulting lists, the Java-side counting and the sublisting operations can be dangerous for the memory usage and consequently also on the performance side. Instead, I usually go with the following approach: use 2 queries, one for counting the filtered resultSet (I let the db do the count), and another one … Read more

@OrderColumn annotation in Hibernate 3.5

The combination of @OneToMany(mappedBy=”…”) and @OrderColumn is not supported by Hibernate. This JIRA issue tracks a request to throw a more obvious error message when this invalid combination is used: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5390 I think that this isn’t supported mainly because it is an odd relational pattern. The annotations above indicate that the “one” side of the … Read more

Another Repeated column in mapping for entity error

The message is clear: you have a repeated column in the mapping. That means you mapped the same database column twice. And indeed, you have: @Column(nullable=false) private Long customerId; and also: @ManyToOne(optional=false) @JoinColumn(name=”customerId”,referencedColumnName=”id_customer”) private Customer customer; (and the same goes for productId/product). You shouldn’t reference other entities by their ID, but by a direct reference … Read more

How do you create a Distinct query in HQL

Here’s a snippet of hql that we use. (Names have been changed to protect identities) String queryString = “select distinct f from Foo f inner join foo.bars as b” + ” where f.creationDate >= ? and f.creationDate < ? and b.bar = ?”; return getHibernateTemplate().find(queryString, new Object[] {startDate, endDate, bar});

How to mix inheritance strategies with JPA annotations and Hibernate?

According to the Hibernate Reference Documentation it should be possible to mix different inheritance mapping strategies when using Hibernate’s XML-Metadata (…) Actually, it’s not really supported, they are “cheating” using a secondary table to switch from the single table strategy in the example of the documentation. Quoting Java Persistence with Hibernate: You can map whole … Read more

Why is HibernateDaoSupport not recommended?

Using HibernateDaoSupport/HibernateTemplate is not recommended since it unnecessarily ties your code to Spring classes. Using these classes was inevitable with older versions of Hibernate in order to integrate support of Spring-managed transactions. However, since Hibernate 3.0.1 you don’t need it any more – you can write a code against a plain Hibernate API while using … Read more

What is the difference between DAO and Repository patterns?

DAO is an abstraction of data persistence. Repository is an abstraction of a collection of objects. DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain, dealing only in Aggregate Roots. Repository could be implemented using DAO‘s, but you wouldn’t do the opposite. Also, a Repository is … Read more

Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

While similar names, the usage is different. I. Projections.distinct(Projections.property(“id”)); this statement would be translated into SQL Statement. It will be passed to DB Engine and executed as a SQL DISTINCT. See: 17.9. Projections, aggregation and grouping so e.g. this example: List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.distinct(Projections.property(“id”)) ) ) .list(); would seems like: SELECT … Read more