Spring Roo and EhCache - ehcache

roo-1.3.1.RC1.
I created simple 3-4 tables with proper PK and FK relations.
I loaded data into it.
I use spring-roo - reverse engg techniques to generate the JSF based UI.
I have a List of Employees which are showing in the default JSF generated page with proper pagination.
if the no of employees is very hing in DB, and If I want to load in to ehCache / hibernate 2nd level cache during application start up - then how Can I able to do that ?
ehCache provides cache warm up interface to load the data into cache before cache is available to the Application?
How Can i achieve this ?

Spring Roo generates (almost) simple Spring + Hibernate application. So, you just must follow the hibernate documentation to configure 2nd level cache. Roo will not disturb on any Hibernate annotation in your classes or changes in the persistence.xml file.
Here you will find a tutorial about it. Sure there are tons of information about it.
Good luck!

Related

ResultSet caching in Spring-boot Camel SQL

I have implemented a Spring-boot Camel batch application running every 5 mins which is using camel-sql component to query some data from table. I am using java DSL implementation and configuring my routing endpoints inside RouteBuilder.configure. Everything is working fine as expected. But now as part of optimization I am planning to cache some of the query results which are not changing frequently like some global configuration tables, location timezone table etc.
I have gone through Camel EHcache documentation, but not getting the right understanding of the standard way of implementation.
My expectation is like the following
Suppose my routing is,
from(timercomponent)
.to(SqlComponent1)
.process(ResultProcessor::processResult1)
.to(SqlComponent2) // needs to be cached
.process(ResultProcessor::processResult1)
....
...
I don't want SqlComponent2 to hit the DB always and it should use a cached value from the first execution with an expiry time of 1 week.
Also I just wanted to know which is the best cache implementation to be used in this scenario.
What do you mean? Ehcache or something else? Or how to configure the Ehcache cache?
From the Ehcache point of view, you will need a Cache using a 1 week TTL expiry.
Then, it's been a while I've implemented caching with Camel but the doc seems to say that an Ehcache idempotent repository should do the trick.

Edit History on Database using Spring

I would like to know if a framework or a component (or even a Spring project!) exists that would allow me to save the history (automatically, or via #Java tags) of all edits done on a specific model object within my Spring project.
Thanks in advance,
Sammy
You are looking for a tool for auditing like Hibernate Envers. It works like a revision system for your DB entities.
Hibernate's integration with Spring is pretty straight forward, as well.
See also:
Hibernate Envers Docs

CRUD generator for Struts 2 application

I have an already existing application built on Java 5 & Struts2 MVC framework having Oracle 11g DB.
Application has large number of tables which are often updated using insert queries or update statements
and I am looking to develop a CRUD application and avoid any manual script execution.
Number of tables are very large with dependency over each other, so I was looking for a java framework which can directly generate the code based on existing table structure, with the flexibility to allow upload excel with data and provide a bulk update features.
Tried with Spring Roo but want to know if there are more framework which can be used with existing Struts 2 MVC.
If you want to use CRUD application with Struts2 then you should look into Struts 2 CRUD Tutorial .
The application example from Struts Struts 2 CRUD Example
This example was created to be as simple as possible and as such, it does not use all of the advanced (integration) features such as Spring IoC, Hibernate Open-session-in-view, OS Sitemesh, annotations, etc .
You can also look at other Examples at GitHub.
About java code generation you can use Jboss Tools.
Hibernate Tools provides easy generation, testing and prototyping of your Hibernate or JPA mapped projects. Use it to Run queries, browse mappings and generate code for your data projects.
Consider Reverse Engineering section if you work on generating Java code.
Put Hibernate Tools Reference Guide to the desktop to read and help with any aspects of code-generation with Hibernate.
References:
Struts2 Builder Project
Easy CRUD with Struts 2 video
Basically what you want is :
Given an existing database, avoid coding manually the queries and the Java classes, and let instead {SMART_SOFTWARE} generates them for you.
Struts2 is a front-end MVC framework, it doesn't know anything specific about CRUD; while using Struts2, you are free to choose any persistence technology you like (JDBC, Hibernate, JPA, etc...).
Since the question involves Java EE, the standard is JPA.
If you're ok with using the Java Persistence API, then you're lucky! The software you're looking for is
Eclipse JPA Tools
It's able to map the DB and create the Java classes (JPA Entities) by reverse-engineering the tables.
Note: there are equivalent tools for NetBeans and other IDE, if you're into them.

How exactly does one integrate a cache in his project (spring + hibernate)

I have a project that supports 10's of concurrent users.
My project is a spring + hibernate project with a MYSQL DB.
I would like to keep a cache for some of my entities (i.e. Player)
I have a couple of questions:
1) how do I exactly work with a cache (when i have one) ? if i have to persist a Player do i change the cache and immediately persist with hibernate?
2) Does spring support a cache mechanism ? if so how does one work with it?
Hibernate
Hibernate has Second level cache. To start work with you need to:
choose some cache provider (EHCache, Infinispan, ...).
configure cache region (and choose corresponding strategy depending on your situation)
enable cache for some entity
It is declarative, most of the time you do not need to change any application code.
Spring
Spring has cache abstraction. There are some common steps (choose chache provider, configure cache regions). But it is more general pourpose cache, not related to Hibernate entities and transactions. So you must do more work (annotate all necessary methods with annotations).
In general if your Player class is a Hibernate entity then it will be better to go with Hibernate cache. It may be not true if you have some special demands.
Hope it helps.

Which is best Spring Cache Abstraction or Hibernate EHCache?

I am planning to implement the Cache in the web application.
I have already used the Hibernate EHCache in one of my previous web application.
Now a days, recently i came to know that in latest Spring release there is Cache Abstraction layer.
Hence i would like to is it just wrapper of Hibernate EHCache or it is Cache features provided by Spring itself.
If different then would like to know which is best or better option to use Cache ?
Also, i came to know from few articles that Hibernate EHCache not supporting clustering, is it ? Because clustering required for big Web Application.
I have used Spring 3.1 and Hibernate 3.3 in Web Application.
Hibernate and EHCache are different things.
EHCache is the cache provider, i.e. the actual implementation of the cache.
Hibernate can use multiple different providers for the L2 cache, including EHCache.
Spring Cache is a framework for caching method calls. It can use multiple different providers, including EHCache.
EHCache offers distribution (clustering) in several modes, from basic JMS-driven synchronization to Terracotta BigMemory distribution. Hibernate L2 cache should support clustering.
Spring Cache has nothing to do with Hibernate, but you can use Hibernate L2 cache and Spring Cache (method caching) in parallel with EHCache as the underlying provider for both.
Just to add, for database-level caching using hibernate, hibernate/JPA second-level cache seems more appropriate. Because it manages data caching at entity level at the persistence context level (JPA APIs), so there are very fewer chances of inconsistency in database and cache, since hibernate manages it well.
But if we use spring cache for database caching, then we always have to ensure every method operating on database need to be annotated with appropriate cache annotation (For example, #CacheEvict for remove), so there are chances of missing annotations and causing inconsistency.
Also, spring cache can only be used on public methods, since it backed my AOP (We can use AspectJ though to solve this).
Hibernate caching continues to work with Spring using EhCache (or another caching provider). It offers a 2nd level cache for storing em.find(key) entities and relationships (#Cache annotations or XML), as well as query caching if you q.setHint("org.hibernate.cacheable",true). These are integrated and well-documented. Spring caching is not needed for this.
But Spring provides method caching which is independent of Hibernate caching, and can still use EhCache as the caching provider (sharing EhCache with Hibernate). This can be a little confusing because the EhCache configurations are overlapping, but essentially Spring Caching allows you to independently cache the results of methods that are marked as #Cacheable.
Note 1: Spring/Method caching is not integrated with Hibernate. Results cached with method caching are not recognized or maintained by Hibernate.
Note 2: Hibernate Query caching is not recommended for highly distributed systems without a good understanding of how the caching is used. It increases the risk of deadlocks due to the use of a shared entity timestamp cache that Hibernate maintains to keep track of when the cached query results should be evicted.

Resources