How to Enable query cache in JBoss 7.1.1 - java

I have tried to enable query cache without luck in JBoss 7.1.1
I have added this in the code:
TypedQuery<Currency> query = entityManager.createNamedQuery("getCurrency",Currency.class);
query.setParameter("code", code);
query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
query.setHint("org.hibernate.cacheable", true);
The namedQuery looks like this:
#Cacheable
#Entity
#Table(name = "currency")
#NamedQuery(
name = "getCurrency",
query = "FROM Currency c WHERE c.iso4217code = :code"
)
I have in my persistence.xml the following:
<persistence-unit name="cache_persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/cache</jta-data-source>
<class>com.unwire.cache.model.CacheTest</class>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="none" />
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
</properties>
</persistence-unit>
When I run the war file on the server
The entity gets cached but the query cache is never used
I have uploaded the file here: http://www.filedropper.com/cachetest

In my code (ear archive, but this should not make a difference) I simply have:
query.setHint("org.hibernate.cacheable", true);
query.setHint("org.hibernate.cacheMode", "NORMAL");
and
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true" />
</properties>
and without any additional things (like you have) and it works. Of course you must test the caching against the same parameters for the query.

Related

unit testing: jpa, hibernate - how to handle entity managers?

I have a test class for a programme that uses jpa/hibernate, the dao pattern, controller classes and a gui - the usual.
I have written a persistence.xml that contains persistence-units for both the main and the test classes:
<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="com.to.me.project.main"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/database" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.id.new_generator_mappings" value="true"/>
<!-- <property name="hibernate.generate_statistics" value="true" /> -->
</properties>
</persistence-unit>
<persistence-unit name="com.to.me.project.test"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:unit-testing" />
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Works fine in production code and when I run the test, the right persistence configuration is found and the databases are created.
My programme works like this: the controller calls a method of the DAO (such as create) and in this method, I get the entitymanager and use its methods (such as persist):
#Override
public void create(final SomeEntity object) throws DAOException {
EntityManager em = MyPersistenceManager.getEntityManager();
em.getTransaction().begin();
em.persist(objekt);
em.getTransaction().commit();
em.close();
}
(The PersistenceManager is my own class, a wrapper around the EntityManagerfactory and so on...)
The problem: That entity manager uses the persistence-unit configs for the main-classes, i. e. the mysql database.So when I run my test and call the controller, it doesnt use the hsqldb databases I set up.
How do I test my controller logic with the configurations for the test persistence unit, i. e. the hsqldb?

Check For Unmapped Columns In Hibernate Mapping

I'm writing a database synchronization tool for existing databases. One of the requirement is to validate the entities with the database schema. The program has to be aware of any database changes (like adding new columns to the database). I'm planning on using Hibernate for this project.
Is there anyway Hibernate can validate the difference between the entity and the table in the database?
Can Hibernate output SQL script so I can review the SQL script before running it to update the target database?
Yes hibernate can output the SQL in the logs.
You need to set the : - <property name="hibernate.show_sql" value="true"/>
but SQL generated by the server is little messy so you have to modify the column name as per your database table.
please have a look at the following XML of my project with the hibernate
<?xml version="1.0" encoding="UTF-8"?>
<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="communityPortalPersitenceUnit">
<class>com.googlecode.frameworksintegration.domain.Blog</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/conhint_prod" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
<property name="hibernate.show_sql" value="true"/>
<!--Start :- connection pooling -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />
<!--End :- connection pooling -->
</properties>
</persistence-unit>
</persistence>

Hibernate don't update entity after changing something directly in database?

Hi all I have pretty strange problem let me first show my configuration etc. Here is persitance.xml:
<persistence-unit name="allegroTransactionPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/AllegroShop?UseUnicode=true&characterEncoding=utf8" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="topSecret" />
<property name="hibernate.connection.password" value="topSecret" />
<!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="show_sql" value="true" />
<property name="hibernate.hbm2ddl.import_files" value="/SQL/payment_type.sql"/>
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />
<property name="hibernate.connection.charSet" value="UTF-8" />
</properties>
</persistence-unit>
And here is method that I use to take data from my database:
#PersistenceContext( unitName = "allegroTransactionPersistenceUnit", type= PersistenceContextType.EXTENDED )
protected EntityManager em;
public List<AllegroTransactionImpl> readAllegroTransactionByCreateDate()
{
TypedQuery<AllegroTransactionImpl> query = this.em.createQuery( "SELECT allegroTransaction FROM com.springapp.mvc.classes.AllegroTransactionImpl allegroTransaction ORDER BY createDate DESC", AllegroTransactionImpl.class);
return query.getResultList();
}
And now the problem. Everything works great if I use hibernate to update / delete / add. but if I change some value directly in base (by SQL statement) hibernate don't refresh it question is why ?
Entity class
https://gist.github.com/spec8320/144100f049f54b73ad86
please make sure you had committed the change by SQL statement.
please check if you have enabled the 2nd level cache on AllegroTransactionImpl

JPA CreateEntityManagerFactory() method hangs

I am using this method in the following way
EntityManagerFactory emftemp = Persistence.createEntityManagerFactory("XYZ");
and my persistence.xml has the following entry for this unit
<persistence-unit name="XYZ" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.query.substitutions" value="true=1, false=0"/>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:#aesop-db.corp.nlg1.com:1521:TLCSDEV4"/>
<property name="hibernate.connection.username" value="abcd"/>
<property name="hibernate.connection.password" value="abcd"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
<property name="hibernate.connection.timeout" value="120"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.min_size" value="2"/>
<property name="hibernate.c3p0.max_size" value="2"/>
<property name="hibernate.c3p0.idle_test_period" value="60"/>
</properties>
</persistence-unit>
This is a Java application and I am using the hibernate3.jar and I am using ejb3-persistence.jar and JDK1.5.
The problem is that everytime I run the application, it hangs on this call . Its not throwing any exception or error . It just hangs at that point.
Can anyone help in identifying why it does not move forward ?
Two thoughts:
When something hangs it's often IO/networking issues to blame. So, try to make sure that the DB is really alive, and that you can connect to it from your computer.
This could be a duplicate of Persistence.createEntityManagerFactory takes ages
I had this same issue against an Oracle 11g database with only three entities defined but a very large DB. After commenting out the following line from persistence.xml, the issue was resolved.
<property name="hibernate.hbm2ddl.auto" value="update"/>

Hibernate/JPA: Mapping entities to different databases

I have an application which manages 3 databases. I use hibernate with JPA on seam framework.
So I have a persitence.xml file with three persitence-unit like this (I remove properties for db2 and db3):
<persistence-unit name="db1" transaction-type="JTA" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>db1source</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class"
value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.default_schema" value="SI_TEC" />
<property name="hibernate.validator.apply_to_ddl" value="false" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.WeblogicTransactionManagerLookup" />
</properties>
</persistence-unit>
<persistence-unit name="db2" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d2source</jta-data-source>
</persistence-unit>
<persistence-unit name="db3" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>d3source</jta-data-source>
</persistence-unit>
In my seam components.xml file, I create 3 managed-persistence-context to map seam with my hibernate configuration.
Finally, I have several entities and my problem is here. I need to persist some entities in db2 and other in db3. So database schema are different and when I deploy my application, I get this error:
org.hibernate.HibernateException: Missing table: PORTAILPERMISSION
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1113)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
Truncated. see log file for complete stacktrace
Because, the table PORTAILPERMISSION doesn't exist in db2.
My question is:
How to specify in entity class what database (or persitence-unit) must be used to validate entity in startup ?
Thanks for your help.
You try to explicitly list classes (<class>..</class>) in each persistence unit. And use
<exclude-unlisted-classes>true</exclude-unlisted-classes>

Categories

Resources