Monday, September 12, 2016

Enabling 2nd level caching in Hibernate/Wildfly

Standalone.xml


User has to provide the hibernate-infinispan library version in the cache-container configuration.



<cache-container name="hibernate" default-cache="local-query" module="org.hibernate.infinispan:5.2.1.Final">
                <local-cache name="entity">
                    <transaction mode="NON_XA"/>
                    <eviction strategy="LRU" max-entries="10000"/>
                    <expiration max-idle="100000"/>
                </local-cache>
                <local-cache name="local-query">
                    <eviction strategy="LRU" max-entries="10000"/>
                    <expiration max-idle="100000"/>
                </local-cache>
                <local-cache name="timestamps"/>
            </cache-container>


Persistance.xml


 <persistence-unit name="apidb-persistence-unit"
            transaction-type="JTA">
            <description>Forge Persistence Unit</description>
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:jboss/datasources/lobDS</jta-data-source>     
            <shared-cache-mode>ALL</shared-cache-mode>
    <properties>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>     </properties>
        </persistence-unit>

Wednesday, September 7, 2016

De/serializing LocalDateTime Using Jackson

java.time.LocalDateTime which represents a date-time as year-month-day-hour-minute-second, is introduced in jdk8.
When we deserialise the objects which contains LocalDateTime , using jackson, it will give comma separated values.


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
 
ObjectMapper mapper = new ObjectMapper();


JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

mapper.registerModule(javaTimeModule);


And also you need to add following dependency;

<dependency>
 <groupid>com.fasterxml.jackson.datatype</groupid>
 <artifactid>jackson-datatype-jsr310</artifactid>
 <version>2.6.5</version>
<dependency>