Skip to content

Commit

Permalink
upgrade error prone and hibernate
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Aug 4, 2023
1 parent 6f0f14e commit 30b3813
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3974,6 +3974,7 @@ private Object writeReplace() {
}
}

@SuppressWarnings("NullableOptional")
static final class BoundedPolicy<K, V> implements Policy<K, V> {
final BoundedLocalCache<K, V> cache;
final Function<V, V> transformer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
45 changes: 45 additions & 0 deletions examples/hibernate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
The [Hibernate ORM] can be configured to use a [second-level cache] to reduce the number of accesses
to the database by caching data in memory to be shared between sessions.

In [hibernate.conf](src/main/resources/hibernate.properties) specify the JCache provider and enable
the second-level cache.

```ini
hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=jcache
hibernate.javax.cache.provider=com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider
```

The caches are configured in Caffeine's [application.conf](src/main/resources/application.conf)
following the [reference format](../../jcache/src/main/resources/reference.conf). The default file
path may be overridden by setting the `hibernate.javax.cache.uri` in the previous step.

```hocon
caffeine.jcache {
default {
policy.maximum.size = 500
}
# Hibernate framework caches
default-query-results-region {}
default-update-timestamps-region {}
# Hibernate application caches
com.github.benmanes.caffeine.examples.hibernate.User {}
}
```

Hibernate will then manage the cache to transparently avoid database calls.

```java
// miss on first access
sessionFactory.fromSession(session -> session.get(User.class, id));
assertThat(sessionFactory.getStatistics().getSecondLevelCacheMissCount()).isEqualTo(1);

// hit on lookup
sessionFactory.fromSession(session -> session.get(User.class, id));
assertThat(sessionFactory.getStatistics().getSecondLevelCacheHitCount()).isEqualTo(1);
```

[Hibernate ORM]: https://hibernate.org/orm/
[second-level cache]: https://docs.jboss.org/hibernate/orm/6.3/introduction/html_single/Hibernate_Introduction.html#second-level-cache-configuration
2 changes: 1 addition & 1 deletion examples/hibernate/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
caffeine = "3.1.7"
h2 = "2.2.220"
hibernate = "6.2.7.Final"
hibernate = "6.3.0.CR1"
junit = "5.10.0"
slf4j = "2.0.7"
truth = "1.1.5"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,39 @@ public Repository(SessionFactory sessionFactory) {
}

public Project getProject(long id) {
try (var session = sessionFactory.openSession()) {
return session.get(Project.class, id);
}
return sessionFactory.fromSession(session -> session.get(Project.class, id));
}

public User getUser(long id) {
try (var session = sessionFactory.openSession()) {
return session.get(User.class, id);
}
return sessionFactory.fromSession(session -> session.get(User.class, id));
}

public Project findProject(long id) {
try (var session = sessionFactory.openSession()) {
var query = session.createQuery("FROM Project WHERE id = :id", Project.class);
query.setParameter("id", id);
return query.uniqueResult();
}
return sessionFactory.fromSession(session ->
session.createQuery("FROM Project WHERE id = :id", Project.class)
.setParameter("id", id)
.uniqueResult());
}

public List<Project> findProjects() {
try (var session = sessionFactory.openSession()) {
return session.createQuery("FROM Project", Project.class).list();
}
return sessionFactory.fromSession(session ->
session.createQuery("FROM Project", Project.class).list());
}

public void updateProject(long id, String name) {
try (var session = sessionFactory.openSession()) {
var txn = session.beginTransaction();
sessionFactory.inTransaction(session -> {
var project = session.get(Project.class, id);
project.setName(name);
session.merge(project);
txn.commit();
}
});
}

public void persist(User user, Project project, Skill skill) {
try (var session = sessionFactory.openSession()) {
var txn = session.beginTransaction();
sessionFactory.inTransaction(session -> {
session.persist(project);
session.persist(skill);
session.persist(user);
txn.commit();
}
});
}

public void evictProject(long projectId) {
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ coveralls = "2.12.2"
dependency-check = "8.3.1"
eclipse-collections = "12.0.0.M2"
ehcache3 = "3.10.8"
errorprone-core = "2.20.0"
errorprone-core = "2.21.0"
errorprone-plugin = "3.1.0"
errorprone-support = "0.12.0"
expiring-map = "0.5.10"
Expand Down Expand Up @@ -50,7 +50,7 @@ jcache = "1.1.1"
jcommander = "1.82"
jctools = "4.0.1"
jfreechart = "1.5.4"
jmh-core = "1.36"
jmh-core = "1.37"
jmh-plugin = "0.7.1"
jmh-report = "0.9.0"
joor = "0.9.14"
Expand Down Expand Up @@ -81,7 +81,7 @@ snakeyaml = "2.0"
sonarqube = "4.3.0.3225"
spotbugs-contrib = "7.6.0"
spotbugs-core = "4.7.3"
spotbugs-plugin = "5.1.0"
spotbugs-plugin = "5.1.1"
stream = "2.9.8"
tcache = "2.0.1"
testng = "7.8.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ val enabledChecks = listOf(
"MissingDefault",
"MixedArrayDimensions",
"MissingDefault",
"MutableGuiceModule",
"NoAllocation",
"OverridingMethodInconsistentArgumentNamesChecker",
"PackageLocation",
"PreferredInterfaceType",
"PreferJavaTimeOverload",
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit 30b3813

Please sign in to comment.