I am developing a website on TomEE, and I want to use the EclipseLink Query Results Cache (L2) working, but every time I reload my web page, SELECT query is running (checked via general_log of mysql).
My Database entity looks like below:
#Entity(name="item")
#NamedQueries({
#NamedQuery(name="ItemEntity.getAllList",
query="Select distinct itemEntity from mypackage.entity.ItemEntity itemEntity",
hints={
#QueryHint(name="eclipselink.query-results-cache", value="true"),
#QueryHint(name="eclipselink.query-results-cache.size", value="1000"),
#QueryHint(name="eclipselink.query-results-cache.expiry", value="10000"), //10 secs for test but not working
#QueryHint(name="eclipselink.query-results-cache.type", value="FULL")
}
)
})
#org.eclipse.persistence.annotations.Cache(
type= CacheType.FULL,
size=10000,
expiry=60000, // 1 minute for test
coordinationType= CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS
)
public class ItemEntity implements Serializable, Comparable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name="name", unique=true, nullable=false)
private String name;
/* getters and setters for fields */
public CompanyEntity(){}
#Override
public int compareTo(Object o) {.......}
}
My persistence.xml looks like below:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA">
<jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<property name="eclipselink.cache.shared.default" value="true"/>
</properties>
</persistence-unit>
</persistence>
I read database table like below:
#Stateful
public class CompanyDao {
#PersistenceContext(unitName = "myprojectname-persistence-unit", type = PersistenceContextType.EXTENDED)
protected EntityManager em;
public List<CompanyEntity> getAllList(){
return this.em.createNamedQuery("ItemEntity.getAllList", ItemEntity.class).getResultList();
}
}
Dependency version details:
TomEE 1.7.2, Java EE6, openJPA 2.4.0, openEJB Java EE API 6.0-6, openEJB core 4.7.2, EclipseLink 2.6.2, MySQL 5.6.23, MySQL connector/J 5.1.38 (Tomcat connection pool)
I've looked at a similar question:
Can't get Eclipselink level 2 cache to work
but it doesn't describe how the OP managed to cache query results.
BTW, the default cache (L2), with em.find(ItemEntity.class, id); is working just fine.
What am I missing? Please help me out.
Solved.
I was using EclipseLink 2.6.2, I downgraded to version 2.4.2, and now Query Results Cache WORKS as expected!
I guess EclipseLink 2.6.x or 2.5.x is not quite compatible with JPA 2.0.x.
It's confusing, because when I use 2.5.x or higher, those still seem to be working, BESIDES the feature of Query Results Cache.
My persistence.xml looks like bellow now, so I can get log output:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA">
<jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<property name="eclipselink.logging.logger" value="JavaLogger"/>
<!-- The warning log of "Problem while registering MBean: java.lang.NullPointerException" did not go away even if I set bellow 2 propertes -->
<!--
<property name="eclipselink.register.dev.mbean" value="false" />
<property name="eclipselink.register.run.mbean" value="false" />
-->
<property name="eclipselink.cache.shared.default" value="true"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.timestamp" value="true"/>
<property name="eclipselink.logging.session" value="true"/>
<property name="eclipselink.logging.thread" value="true"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
Related
As seen in below code in step1 I'm reading users.xml and writing to database now in step2 I'm reading from userdetails.xml and writing to database but I need step1 auto generated key of tbl_user for step2. How Can I do that?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<import resource="../config/context.xml" />
<import resource="../config/database.xml" />
<bean id="xmlItemReader1" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="resource" value="file:xml/outputs/users.xml" />
<property name="fragmentRootElementName" value="user" />
<property name="unmarshaller" ref="userUnmarshaller"/>
</bean>
<bean id="xmlItemReader2" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="resource" value="file:xml/outputs/userdetails.xml" />
<property name="fragmentRootElementName" value="userdetail" />
<property name="unmarshaller" ref="userUnmarshaller"/>
</bean>
<bean id="itemProcessor1" class="com.qmetry.recovery.mapper.UserItemProcessor" />
<bean id="itemProcessor2" class="com.qmetry.recovery.mapper.UserDetailItemProcessor" />
<job id="testJob2" xmlns="http://www.springframework.org/schema/batch">
<step id="step2_1">
<tasklet transaction-manager="transactionManager">
<chunk reader="xmlItemReader1" writer="databaseItemWriter1" processor="itemProcessor1"
commit-interval="100" />
</tasklet>
<listeners>
<listener ref="testListener" />
</listeners>
</step>
<step id="step2_2">
<tasklet transaction-manager="transactionManager">
<chunk reader="xmlItemReader2" writer="databaseItemWriter2" processor="itemProcessor1"
commit-interval="100" />
</tasklet>
</step>
</job>
<bean id="testListener" class="com.qmetry.recovery.mapper.TestListener" scope="step" />
<bean id="databaseItemWriter1" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
insert into TBL_USER(USERNAME,EMAILID)
values (?, ?)
]]>
</value>
</property>
<!--We need a custom setter to handle the conversion between Jodatime LocalDate and MySQL DATE BeanPropertyItemSqlParameterSourceProvider-->
<property name="itemPreparedStatementSetter">
<bean class="com.qmetry.recovery.mapper.UserItemPreparedStatementSetter"/>
</property>
</bean>
<bean id="databaseItemWriter2" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
insert into TBL_USERDETAIL(USERID,CONTACT)
values (?, ?)
]]>
</value>
</property>
<!--We need a custom setter to handle the conversion between Jodatime LocalDate and MySQL DATE BeanPropertyItemSqlParameterSourceProvider-->
<property name="itemPreparedStatementSetter">
<bean class="com.qmetry.recovery.mapper.UserDetailItemPreparedStatementSetter"/>
</property>
</bean>
users.xml
<?xml version="1.0" encoding="UTF-8"?><users>
<user>
<userId>1</userId>
<userName>Taher</userName>
<emailId>taher.tinwala#hotmail.com</emailId>
</user>
</users>
userdetails.xml
<?xml version="1.0" encoding="UTF-8"?><userdetails>
<userdetail>
<userDetailId>1</userDetailId>
<userId__TblUser>1</userId__TblUser>
<contact>1111111111</contact>
</userdetail>
<userdetail>
<userDetailId>2</userDetailId>
<userId__TblUser>1</userId__TblUser>
<contact>2222222222</contact>
</userdetail>
<userdetail>
<userDetailId>4</userDetailId>
<userId__TblUser>1</userId__TblUser>
<contact>4444444444</contact>
</userdetail>
</userdetails>
You need to pass data to a future step. For explantory documentation see http://docs.spring.io/spring-batch/trunk/reference/html/patterns.html#passingDataToFutureSteps
I have implemented the example from the documentation and adjusted it to your configuration with some assumptions here and there.
During the read (or the write, it depends when you get the data that you want to pass) in step 1 you need to store the data in the StepExecution. Add to your xmlItemReader the following:
public class YourItemReader implements ItemReader<Object>
private StepExecution stepExecution;
public void read(Object item) throws Exception {
// ...
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("tbl_user", someObject);
}
#BeforeStep
public void saveStepExecution(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
Your xml will look like this:
<step id="step2_1">
<tasklet transaction-manager="transactionManager">
<chunk reader="xmlItemReader1" writer="databaseItemWriter1" processor="itemProcessor1" commit-interval="100" />
</tasklet>
<listeners>
<listener ref="testListener" />
<listener ref="promotionListener"/>
</listeners>
</step>
Add the promotionListener bean:
<beans:bean id="promotionListener" class="org.springframework.batch.core.listener.ExecutionContextPromotionListener">
<beans:property name="keys" value="tbl_key"/>
</beans:bean>
And finally you need to retrieve the value in step 2. Again asuming you need it in the reader of step 2 you reader in step 2 needs the following code added:
public class YourItemReader2 implements ItemReader<Object>
private Object someObject;
#BeforeStep
public void retrieveInterstepData(StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
this.someObject = jobContext.get("tbl_key");
}
Now you have acces to the value read in step 1.
EDIT - adding some example configuration for an extra read step:
After step 1 add a simple step 2 with the following reader to get the new value from the database
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
YOUR SELECT STATEMENT
]]>
</value>
</property>
<property name="rowMapper" ref="rowMapper" />
</bean>
And a simple rowMapper bean
<bean id="rowMapper" class="exampleRowMapper" />
You will have to write your exampleRowMapper obviously to reflect the data your fetching. For example:
public class ExampleRowMapper implements ParameterizedRowMapper<String> {
#Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return String.valueOf(rs.getString(1));
}
}
In your dummywriter you add the stepexecution and you will store your value in the step execution context.:
public class DummyItemWriter implements ItemWriter<Object> {
private StepExecution stepExecution;
#Override
public void write(List<? extends Object> item) throws Exception {
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("someKey", someObject);
}
}
And the bean for the writer:
<bean id="savingDummyWriter" class="your.package.DummyItemWriter" />
And wrap the reader and writer in a step.
<step id="step2">
<tasklet>
<chunk reader="itemReader" writer="dummyItemWriter" commit-interval="1" />
</tasklet>
<listeners>
<listener ref="promotionListener"/>
</listeners>
</step>
I'm integrate spring with hibernate , I'm using dropwizard configaration files as : persistence file have the configuration files are in resources folder no issue with loading xml files. There is issue with found table .
persistence.xml file as :
<persistence-unit name="bfm" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.bcits.parkingmanagement.model.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Configuring JDBC properties -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/anuj" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
</persistence-unit>
</persistence>
In spring application config xml as : there I show only hibernate configuration
<context:annotation-config />
<context:component-scan base-package="com.bcits.parkingmanagement.model" />
<jdbc:embedded-database id="dataSource" type="HSQL" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:spring/persistence.xml" />
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Table User missing
javax.persistence.PersistenceException: [PersistenceUnit: bfm] Unable to build EntityManagerFactory
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
... 18 more
Caused by: org.hibernate.HibernateException: Missing table: user
Please anyone help me how can I resolve this error. I want to configuration of spring with hibernate.I thing it is connect to database but can't found table. Table name and colume name exactly same. Database name also correct and no issue with username and password of database .
User model is: Here User is model name and user is table which have two column one is user_id and user_name. Issue is that i had used user instead of User in query. Because User is model name and user is table name .Correct model is given below
#NamedQueries({
#NamedQuery( name="getUserName" , query="select name from User")
})
#Entity
#Table( name ="user")
public class User {
#Id
#Column( name="user_id")
private int id;
#Column( name="user_name")
private String name;
//getter setter
}
I'm getting the exception above while running a JUnit/Arquillian test in Eclipse Juno having a #Deployment method like this:
#Deployment
public static Archive<?> createDeployment() throws Exception {
File[] libs = Maven.resolver()
.loadPomFromFile("pom.xml")
.importRuntimeDependencies()
.as(File.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
.addClass(Permission.class)
.addClass(PermissionInterface.class)
.addClass(PermissionModel.class)
.addClass(PermissionModelInterface.class)
...
.addAsLibraries(libs)
.addAsResource(
"resources-jbossas-managed/test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebResource(
EmptyAsset.INSTANCE,
ArchivePaths.create("beans.xml"));
System.out.println(war.toString(true));
return war;
}
and a persistence unit described like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="talmudDatabaseTest">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/Alpha2Test</jta-data-source>
<properties>
<property name="eclipselink.target-server" value="JBoss" />
<property name="eclipselink.target-database" value="MySQL" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.deploy-on-startup" value="True" />
<property name="eclipselink.session-event-listener" value="it.cnr.ilc.omega.model.PersistenceEventListener" />
<property name="omega.import-sql" value="false" />
<property name="omega.import-sql.postlogin-filename" value="preconstruct.sql" />
<property name="omega.import-sql.postcreate-filename" value="postconstruct.sql" />
</properties>
</persistence-unit>
</persistence>
The running JBoss 7.1.1 Final instance has been launched with this configuration (on a Macbook Pro):
-server -Xms512m -Xmx1024m -XX:MaxPermSize=512m -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true "-Dorg.jboss.boot.log.file=/usr/local/jboss-as-7.1.1.Final/standalone/log/boot.log" "-Dlogging.configuration=file:/usr/local/jboss-as-7.1.1.Final/standalone/configuration/logging.properties" "-Djboss.home.dir=/usr/local/jboss-as-7.1.1.Final" "-Dfile.encoding=UTF-8" "-Djboss.modules.system.pkgs=com.apple.laf,com.apple.laf.resources"
The #Test method isn't even called... the exception is thrown before it, but after the completion of the #Deployment method. Shrinkwrap creates this web archive:
test.war:
/beans.xml
/WEB-INF/
/WEB-INF/lib/
/WEB-INF/lib/atmosphere-compat-tomcat-1.1.0.beta3.jar
/WEB-INF/lib/bcprov-jdk14-1.38.jar
/WEB-INF/lib/jackson-databind-2.0.5.jar
/WEB-INF/lib/bcmail-jdk14-1.38.jar
/WEB-INF/lib/maven-aether-provider-3.0.5.jar
/WEB-INF/lib/shrinkwrap-impl-base-1.1.2.jar
/WEB-INF/lib/jcl-over-slf4j-1.6.4.jar
/WEB-INF/lib/shrinkwrap-resolver-spi-maven-2.0.0-beta-3.jar
/WEB-INF/lib/shrinkwrap-resolver-api-2.0.0-beta-3.jar
/WEB-INF/lib/maven-plugin-registry-2.0.9.jar
/WEB-INF/lib/shrinkwrap-spi-1.1.1.jar
/WEB-INF/lib/maven-model-builder-3.0.5.jar
/WEB-INF/lib/plexus-container-default-1.0-alpha-9-stable-1.jar
/WEB-INF/lib/geronimo-stax-api_1.0_spec-1.0.jar
/WEB-INF/lib/maven-plugin-api-2.0.9.jar
/WEB-INF/lib/poi-3.7.jar
/WEB-INF/lib/bcmail-jdk14-138.jar
/WEB-INF/lib/wagon-http-shared4-2.4.jar
/WEB-INF/lib/bcprov-jdk14-138.jar
/WEB-INF/lib/xmlbeans-2.3.0.jar
/WEB-INF/lib/maven-profile-2.0.9.jar
/WEB-INF/lib/poi-ooxml-schemas-3.7.jar
/WEB-INF/lib/aether-util-1.13.1.jar
/WEB-INF/lib/slf4j-log4j12-1.6.4.jar
/WEB-INF/lib/atmosphere-compat-jbossweb-1.1.0.beta3.jar
/WEB-INF/lib/jackson-core-2.0.5.jar
/WEB-INF/lib/commons-io-1.3.2.jar
/WEB-INF/lib/shrinkwrap-resolver-spi-2.0.0-beta-3.jar
/WEB-INF/lib/log4j-1.2.16.jar
/WEB-INF/lib/commons-digester-2.1.jar
/WEB-INF/lib/itext-2.1.7.js1.jar
/WEB-INF/lib/plexus-interpolation-1.14.jar
/WEB-INF/lib/classworlds-1.1-alpha-2.jar
/WEB-INF/lib/slf4j-simple-1.7.5.jar
/WEB-INF/lib/plexus-utils-2.0.6.jar
/WEB-INF/lib/annotation-detector-3.0.1.jar
/WEB-INF/lib/commons-codec-20041127.091804.jar
/WEB-INF/lib/atmosphere-compat-tomcat7-1.1.0.beta3.jar
/WEB-INF/lib/commons-collections-2.1.jar
/WEB-INF/lib/el-api-2.2.jar
/WEB-INF/lib/jdtcore-3.1.0.jar
/WEB-INF/lib/primefaces-3.5.jar
/WEB-INF/lib/commonj.sdo-2.1.1.v201112051852.jar
/WEB-INF/lib/shrinkwrap-resolver-impl-maven-2.0.0-beta-3.jar
/WEB-INF/lib/eclipselink-staticweave-maven-plugin-1.0.4.jar
/WEB-INF/lib/aether-spi-1.13.1.jar
/WEB-INF/lib/aether-impl-1.13.1.jar
/WEB-INF/lib/stax-api-1.0.1.jar
/WEB-INF/lib/jcommon-1.0.15.jar
/WEB-INF/lib/commons-logging-1.1.1.jar
/WEB-INF/lib/wagon-provider-api-2.4.jar
/WEB-INF/lib/xml-apis-1.4.01.jar
/WEB-INF/lib/plexus-compiler-javac-2.1.jar
/WEB-INF/lib/shrinkwrap-api-1.1.1.jar
/WEB-INF/lib/shrinkwrap-resolver-api-maven-2.0.0-beta-3.jar
/WEB-INF/lib/jsoup-1.6.3.jar
/WEB-INF/lib/plexus-component-api-1.0-alpha-33.jar
/WEB-INF/lib/maven-project-2.0.9.jar
/WEB-INF/lib/shrinkwrap-resolver-impl-maven-archive-2.0.0-beta-3.jar
/WEB-INF/lib/httpcore-4.2.3.jar
/WEB-INF/lib/maven-settings-builder-3.0.5.jar
/WEB-INF/lib/shrinkwrap-resolver-api-maven-archive-2.0.0-beta-3.jar
/WEB-INF/lib/maven-artifact-2.0.9.jar
/WEB-INF/lib/poi-ooxml-3.7.jar
/WEB-INF/lib/wagon-http-lightweight-2.4.jar
/WEB-INF/lib/dom4j-1.6.1.jar
/WEB-INF/lib/jena-core-2.10.0.jar
/WEB-INF/lib/slf4j-api-1.6.4.jar
/WEB-INF/lib/eclipselink-2.4.0.jar
/WEB-INF/lib/aether-connector-wagon-1.13.1.jar
/WEB-INF/lib/xercesImpl-2.10.0.jar
/WEB-INF/lib/castor-1.2.jar
/WEB-INF/lib/jfreechart-1.0.12.jar
/WEB-INF/lib/maven-repository-metadata-3.0.5.jar
/WEB-INF/lib/bctsp-jdk14-1.38.jar
/WEB-INF/lib/jasperreports-fonts-4.0.0.jar
/WEB-INF/lib/shrinkwrap-resolver-spi-maven-archive-2.0.0-beta-3.jar
/WEB-INF/lib/jena-tdb-0.10.0.jar
/WEB-INF/lib/commons-lang3-3.0.1.jar
/WEB-INF/lib/maven-artifact-manager-2.0.9.jar
/WEB-INF/lib/plexus-classworlds-2.4.jar
/WEB-INF/lib/maven-settings-3.0.5.jar
/WEB-INF/lib/commons-beanutils-1.8.0.jar
/WEB-INF/lib/jena-arq-2.10.0.jar
/WEB-INF/lib/httpclient-4.2.3.jar
/WEB-INF/lib/javax.persistence-2.0.4.v201112161009.jar
/WEB-INF/lib/aether-api-1.13.1.jar
/WEB-INF/lib/maven-model-3.0.5.jar
/WEB-INF/lib/atmosphere-runtime-1.1.0.beta3.jar
/WEB-INF/lib/plexus-compiler-api-2.1.jar
/WEB-INF/lib/jena-iri-0.9.5.jar
/WEB-INF/lib/jackson-annotations-2.0.5.jar
/WEB-INF/lib/jasperreports-5.0.0.jar
/WEB-INF/lib/wagon-file-2.4.jar
/WEB-INF/classes/
/WEB-INF/classes/META-INF/
/WEB-INF/classes/META-INF/persistence.xml
/WEB-INF/classes/it/
/WEB-INF/classes/it/cnr/
/WEB-INF/classes/it/cnr/ilc/
/WEB-INF/classes/it/cnr/ilc/omega/
/WEB-INF/classes/it/cnr/ilc/omega/resources/
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/SourceInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/Source.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/AttachmentInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/ResourceType.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/ResourceInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/Resource.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/Attachment.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/ResourceTypeInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/
/WEB-INF/classes/it/cnr/ilc/omega/access/LoginViewController.class
/WEB-INF/classes/it/cnr/ilc/omega/access/LoginViewControllerInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Task.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Role.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Credentials.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/PermissionModel.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/TaskInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/UserInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/UserModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/TaskModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/PermissionInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/PermissionModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/User.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/LogoutEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/TaskModel.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Permission.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/UserModel.class
/WEB-INF/classes/it/cnr/ilc/omega/model/
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntityModel.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntityInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/model/EntityDeleteEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/model/EntityCreateEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/model/EntityUpdateEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntityModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntity$Status.class
/WEB-INF/classes/it/cnr/ilc/omega/model/PersistenceEventListener.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntity.class
/WEB-INF/classes/it/cnr/ilc/omega/BaseController.class
/WEB-INF/classes/it/cnr/ilc/omega/Resources.class
/WEB-INF/classes/it/cnr/ilc/omega/reference/
/WEB-INF/classes/it/cnr/ilc/omega/reference/model/
/WEB-INF/classes/it/cnr/ilc/omega/reference/model/Reference.class
/WEB-INF/classes/it/cnr/ilc/omega/reference/model/ReferenceInterface.class
After that, Arquillian adds the archive on the active instance of JBoss, but the deployment can't terminate its operations. I've also managed to raise the memory for the JVM, but it doesn't seem to work. If I remove the libs form the archive, the #Test method is passed correctly, but the Session bean i'd like to inject:
#Inject
LoginViewController loginViewController;
isn't resolved like it should.
Can anyone help to find why the exception occurs? Thanks in advance... any help is appreciated!
.addAsWebResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
should be
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
I am using Hibernate/JPA with mySQL, and because of legacy reasons createNativeQuery at one point. The application works with different servers using the same database, so it should do no caching at all but always show the most recent result. I simulate other servers by changing a value manually in a database-editor, but after a change it always gives old results.
As far as I know I should disable any 2nd level caching (not very important because I do not use any ORM-objects), clear() any 1st level caching, and disable mysql query caching (is already done on database level). Where do I fail, or what do I forget? It drives me crazy.
init(): start of the servlet
entityFactory = Persistence.createEntityManagerFactory("persistence-id");
getEntityManager(): start of each request
destroyEntityManager(); // just in case
entityFactory.getCache().evictAll();
entityManager = entityFactory.createEntityManager();
entityManager.setProperty("javax.persistence.cache.storeMode",
CacheStoreMode.BYPASS);
entityManager.clear(); // just in case
destroyEntityManager(): end of each request
if (entityManager != null) {
if (entityManager.getTransaction().isActive()) {
entityManager.flush();
entityManager.getTransaction().commit();
}
entityManager.clear();
if (entityManager.isOpen()) {
entityManager.close();
}
entityManager = null;
}
destroy(): end of servlet
destroyEntityManager();
if (entityFactory != null) {
entityFactory.close();
}
persistence.xml:
<?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_1_0.xsd" version="1.0">
<persistence-unit name="WallMountBackOffice-PU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>...</class>
<class>...</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/ourschema" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.connection.pool_size" value="10" />
<property name="hibernate.connection.autocommit" value="false" />
<property name="hibernate.connection.release_mode" value="on_close" />
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.cache.use_second_level_cache"
value="false" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="javax.persistence.sharedCache.mode" value="NONE" />
<property name="org.hibernate.cacheable" value="false" />
</properties>
</persistence-unit>
The code which does the 'select ...':
...
Query jpaQuery = entityManager.createQuery(query);
entityManager.getTransaction().begin();
jpaQuery.executeUpdate();
entityManager.getTransaction().commit();
In first line, there is a mistake, it must be "BYPASS" not "REFRESH", as following:
query.setHint("javax.persistence.cache.retrieveMode", "BYPASS");
And using the JPA enums instead of string literals is recommended, so it will be:
query.setHint(QueryHints.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS);
query.setHint(QueryHints.CACHE_STORE_MODE, CacheStoreMode.REFRESH);
You can use setHint() storeMode or retrieveMode method. If you are trying to retrieve the record, use retrieveMode with BYPASS.
For Hibernate
query.setHint("javax.persistence.cache.storeMode", "REFRESH");
query.setHint("javax.persistence.cache.retrieveMode", "REFRESH");
For EclipseLink.
query.setHint("javax.persistence.cache.storeMode", "REFRESH");
query.setHint("javax.persistence.cache.retrieveMode", "REFRESH");
JPA 2.0 Specification
public enum CacheRetrieveMode {
/**
* Read entity data from the cache: this is
* the default behavior.
*/
USE,
/**
* Bypass the cache: get data directly from
* the database.
*/
BYPASS
}
public enum CacheStoreMode {
/**
* Insert/update entity data into cache when read
* from database and when committed into database:
* this is the default behavior. Does not force refresh
* of already cached items when reading from database.
*/
USE,
/**
* Don't insert into cache.
*/
BYPASS,
/**
* Insert/update entity data into cache when read
* from database and when committed into database:
* Forces refresh of cache for items read from database.
*/
REFRESH
}
a sample of json response looks like this:
{"publicId":"123","status":null,"partner":null,"description":null}
It would be nice to truncate out all null objects in the response. In this case, the response would become {"publicId":"123"}.
Any advice? Thanks!
P.S: I think I can do that in Jersey. Also I believe they both use Jackson as the JSON processer.
Added Later:
My configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="com.SomeCompany.web" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!-- Configures Spring MVC -->
<import resource="mvc-config.xml" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the #Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<!--<mvc:view-controller path="/" view-name="welcome"/>-->
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!--<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
<entry key="xml" value="text/xml"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
<property name="marshaller">
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller" />
</property>
</bean>
</list>
</property>
</bean>
-->
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
My code:
#Controller
public class SomeController {
#RequestMapping(value = "/xyz", method = {RequestMethod.GET, RequestMethod.HEAD},
headers = {"x-requested-with=XMLHttpRequest","Accept=application/json"}, params = "!closed")
public #ResponseBody
List<AbcTO> getStuff(
.......
}
}
Yes, you can do this for individual classes by annotating them with #JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) or you can do it across the board by configuring your ObjectMapper, setting the serialization inclusion to JsonSerialize.Inclusion.NON_NULL.
Here is some info from the Jackson FAQ: http://wiki.fasterxml.com/JacksonAnnotationSerializeNulls.
Annotating the classes is straightforward, but configuring the ObjectMapper serialization config slightly trickier. There is some specific info on doing the latter here.
Doesn't answer the question but this is the second google result.
If anybody comes here and wants do do it for Spring 4 (as it happened to me), you can use the annotation
#JsonInclude(Include.NON_NULL)
on the returning class.
As mentioned in the comments, and in case anyone is confused, the annotation should be used in the class that will be converted to JSON.
If using Spring Boot for REST, you can do it in application.properties:
spring.jackson.serialization-inclusion=NON_NULL
source
Java configuration for the above. Just place the below in your #Configuration class.
#Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
return builder;
}