Skip to content

Commit

Permalink
new project showing hibernate second-level caching with spring data j…
Browse files Browse the repository at this point in the history
…ps repositories
  • Loading branch information
zzantozz committed Jun 18, 2013
1 parent c0b9252 commit 43e211b
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 0 deletions.
18 changes: 18 additions & 0 deletions spring-data-hibernate-caching/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply plugin: 'groovy'
apply plugin: 'idea'

repositories { mavenCentral() }
dependencies {
compile(
'org.codehaus.groovy:groovy-all:2.1.1',
'org.hibernate:hibernate-core:3.5.2-Final',
'org.hibernate:hibernate-ehcache:3.5.2-Final',
'org.hibernate:hibernate-entitymanager:3.5.2-Final',
'net.sf.ehcache:ehcache:2.4.7',
'org.springframework:spring-core:3.1.0.RELEASE',
'org.springframework:spring-context:3.1.0.RELEASE',
'org.springframework.data:spring-data-jpa:1.2.0.RELEASE',
'ch.qos.logback:logback-classic:1.0.13',
'com.h2database:h2:1.3.172',
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package rds.testbed;

import org.springframework.context.support.ClassPathXmlApplicationContext;

class Main {
public static void main(String[] args) {
def context = new ClassPathXmlApplicationContext('/applicationContext.xml')
def repo = context.getBean(ThingRepo)
println "Saving bob"
// Like this, won't cache on save because of https://hibernate.atlassian.net/browse/HHH-7964
Thing saved = repo.save(new Thing('bob'))
println "Loading bob"
Thing loaded = repo.findByName('bob')
assert !saved.is(loaded)
assert saved.name == loaded.name
println "bob is: $loaded"
println "Loading bob again"
println "bob is: " + repo.findByName('bob')
}
}
43 changes: 43 additions & 0 deletions spring-data-hibernate-caching/src/main/java/rds/testbed/Thing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rds.testbed;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Thing {
@Id @GeneratedValue
private long id;
private String name;

public Thing() {}

public Thing(String name) {
this.name = name;
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Thing{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package rds.testbed;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.QueryHints;

import javax.persistence.QueryHint;

public interface ThingRepo extends JpaRepository<Thing, Long> {
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
Thing findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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="spring-data-hibernate-caching"/>
</persistence>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
">

<jpa:repositories base-package="rds.testbed" />
<jdbc:embedded-database id="dataSource" type="H2"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:persistenceUnitName="spring-data-hibernate-caching"
p:persistenceProvider-ref="provider">
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- Should be set to some value according to docs, but doesn't seem necessary for this simple test -->
<!--<prop key="javax.persistence.sharedCache.mode">ALL</prop>-->
</props>
</property>
</bean>
<bean id="provider" class="org.hibernate.ejb.HibernatePersistence"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
</beans>
5 changes: 5 additions & 0 deletions spring-data-hibernate-caching/src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ehcache>
<cache name="org.hibernate.cache.StandardQueryCache"/>
<cache name="org.hibernate.cache.UpdateTimestampsCache"/>
<cache name="rds.testbed.Thing"/>
</ehcache>
13 changes: 13 additions & 0 deletions spring-data-hibernate-caching/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="org.hibernate.SQL" level="trace"/>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

0 comments on commit 43e211b

Please sign in to comment.