Hallo,
I discovered a strange behaviour with hibernate. I have a Web-Applikation with JSF and Primefaces, as ORM I use Hibernate 4.2.5.
In my Browser data of table were shown. I change a entry (firstData --> secondData) in a cell, the change was wrong so I renew data. Everything ok (firstData).
I change same entry(firstData --> thirdData), the change was wrong so I renew data. Hibernate give the data of my first change (secondData). There is value "secondData" instead of "firstData" in my table. This behaviour can occur after 3.,4.,5. .. relodaing. So its not always the same.
When Hibernate give back correct data (firstData), Hibernate make a call to database and not to Hibernate itself. But I want to use Hibenrate caching and not fetching data from database everytime.
Here are my abstract DAO:
Code:
@Stateless
@Local(IHibernateDAO.class)
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public abstract class CHibernateDAO<T> implements IHibernateDAO {
private static final ThreadLocal<EntityManager> tl_EntityManager = new ThreadLocal<EntityManager>();
private static final EntityManagerFactory obj_EntityManagerFactory;
private Class<?> persistentClass;
static
{
obj_EntityManagerFactory = Persistence.createEntityManagerFactory("Persistence");
}
public CHibernateDAO(Class<?> c)
{
persistentClass = c;
}
private static EntityManager getCurrentManger()
{
EntityManager obj_manager = null;
obj_manager = (EntityManager)tl_EntityManager.get();
if(obj_manager == null || !obj_manager.isOpen())
{
obj_manager = obj_EntityManagerFactory.createEntityManager();
tl_EntityManager.set(obj_manager);
}
}
public List<T> findAll()
{
List<T> li_Return = null;
CriteriaBuilder cb = getCurrentManger().getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = (CriteriaQuery<T>) cb.createQuery(persistentClass);
Root<T> root = (Root<T>) criteriaQuery.from(persistentClass);
criteriaQuery.select(root);
TypedQuery<T> typedQuery = getCurrentManger().createQuery(criteriaQuery);
li_Return = typedQuery.getResultList();
return li_Return;
}
}
And my persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistenz Descriptor zur Konfiguration -->
<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="Persistence" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.url" value="jdbc:oracle:thin:..."/>
<property name="hibernate.connection.username" value="..."/>
<property name="hibernate.connection.password" value="..."/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.jdbc.batch_size" value="20" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.cache.use_second_level_cache"
value="false" />
</properties>
</persistence-unit>
</persistence>
Hibernate should give me a proxy of real Object. But in my case it seems to get a reference with my changed entry.
Can you give me an hint?
Thanks a lot