-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new project showing hibernate second-level caching with spring data j…
…ps repositories
- Loading branch information
Showing
8 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-data-hibernate-caching/src/main/groovy/rds/testbed/Main.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
spring-data-hibernate-caching/src/main/java/rds/testbed/Thing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' + | ||
'}'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
spring-data-hibernate-caching/src/main/java/rds/testbed/ThingRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
9 changes: 9 additions & 0 deletions
9
spring-data-hibernate-caching/src/main/resources/META-INF/persistence.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
33 changes: 33 additions & 0 deletions
33
spring-data-hibernate-caching/src/main/resources/applicationContext.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
spring-data-hibernate-caching/src/main/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |