How to use JPA2’s @Cacheable instead of Hibernate’s @Cache

According to the JPA 2.0 specification, if you want to selectively cache entities using the @Cacheable annotation, you’re supposed to specify a <shared-cache-mode> in the persistence.xml (or the equivalent javax.persistence.sharedCache.mode when creating the EntityManagerFactory). Below, a sample persistence.xml with the relevant element and properties: <persistence xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd” version=”2.0″> <persistence-unit name=”FooPu” transaction-type=”RESOURCE_LOCAL”> <provider>org.hibernate.ejb.HibernatePersistence</provider> … … Read more

Hibernate hbm2ddl.auto, possible values, and what they do

For hbm2ddl.auto property the list of possible options is: validate: validate that the schema matches, make no changes to the schema of the database, you probably want this for production. update: update the schema to reflect the entities being persisted create: creates the schema necessary for your entities, destroying any previous data. create-drop: create the … Read more

hibernate – get id after save object

The session.save(object) returns the id of the object, or you could alternatively call the id getter method after performing a save. Save() return value: Serializable save(Object object) throws HibernateException Returns: the generated identifier Getter method example: UserDetails entity: @Entity public class UserDetails { @Id @GeneratedValue private int id; private String name; // Constructor, Setters & … Read more

Hibernate validator: @Email accepts ask@stackoverflow as valid?

You can also use constraint composition as a work-around. In the example below, I rely on the @Email validator to do the main validation, and add a @Pattern validator to make sure the address is in the form of [email protected] (I don’t recommend using just the @Pattern below for regular Email validation) @Email(message=”Please provide a … Read more

Date operations in HQL

Depending on your database, this can be trivially simple. HQL supports built-in vendor-specific features and functions, it also supports the ability to extend the dialect by registering new functions if they’re not already supported by HQL. Let’s say you’re using SQLServer (or Sybase). SQLServer has a function called ‘DATEADD’ that can do what you like … Read more

What is the right way to use spring MVC with Hibernate in DAO, service layer architecture

IMHO the transactions should go to service layer. Typically one business transaction consists of several queries and updates. If you place @Transactional only on DAO layer, each query and update will run in a separate transaction, which effectively defeats the purpose of transactions. But if services are @Transactional, each database interaction joins one main transaction … Read more

Hibernate inserts duplicates into a @OneToMany collection

It’s a bug in Hibernate. Surprisingly, it’s not reported yet, feel free to report it. Operations against non-initialized lazy collections are queued in order to execute them after collection is initialized, and Hibernate doesn’t handle the situation when these operations conflict with the data from the database. Usually it’s not a problem, because this queue … Read more

hibernate connection pool

<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”> <hibernate-configuration> <session-factory> <!– datasource config –> <property name=”connection.url”>jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true</property> <property name=”hibernate.dialect”>org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name=”connection.driver_class”>com.mysql.jdbc.Driver</property> <property name=”connection.username”>user</property> <property name=”connection.password”>pass</property> <!– c3p0 config http://www.hibernate.org/214.html –> <property name=”connection.provider_class”>org.hibernate.connection.C3P0ConnectionProvider</property> <property name=”hibernate.c3p0.acquire_increment”>1</property> <property name=”hibernate.c3p0.idle_test_period”>60</property> <property name=”hibernate.c3p0.min_size”>1</property> <property name=”hibernate.c3p0.max_size”>2</property> <property name=”hibernate.c3p0.max_statements”>50</property> <property name=”hibernate.c3p0.timeout”>0</property> <property name=”hibernate.c3p0.acquireRetryAttempts”>1</property> <property name=”hibernate.c3p0.acquireRetryDelay”>250</property> <property name=”hibernate.show_sql”>true</property> <property name=”hibernate.use_sql_comments”>true</property> <property name=”hibernate.transaction.factory_class”>org.hibernate.transaction.JDBCTransactionFactory</property> <property name=”hibernate.current_session_context_class”>thread</property> … Read more

Hibernate unidirectional one to many association – why is a join table better?

Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key references in the owned table to both parent tables? What if you have three parent types? It just doesn’t scale to large designs. A join-table decouples the join, so that the owned table … Read more