-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update hibernate example to use the preferred meta model generator
- Loading branch information
Showing
6 changed files
with
145 additions
and
165 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
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
39 changes: 39 additions & 0 deletions
39
...ples/hibernate/src/main/java/com/github/benmanes/caffeine/examples/hibernate/Queries.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,39 @@ | ||
/* | ||
* Copyright 2023 Ben Manes. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.benmanes.caffeine.examples.hibernate; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.annotations.processing.Find; | ||
import org.hibernate.annotations.processing.HQL; | ||
|
||
/** | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
public interface Queries { | ||
|
||
@Find | ||
User getUser(long id); | ||
|
||
@Find | ||
Project getProject(long id); | ||
|
||
@HQL("FROM Project WHERE id = :id") | ||
Project findProject(long id); | ||
|
||
@Find | ||
List<Project> findProjects(); | ||
} |
77 changes: 0 additions & 77 deletions
77
...s/hibernate/src/main/java/com/github/benmanes/caffeine/examples/hibernate/Repository.java
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -17,109 +17,111 @@ | |
|
||
import static com.github.benmanes.caffeine.examples.hibernate.HibernateSubject.assertThat; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.cfg.Configuration; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
public final class HibernateCacheTest { | ||
SessionFactory sessionFactory; | ||
Repository repository; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
sessionFactory = new Configuration().addAnnotatedClass(User.class) | ||
.addAnnotatedClass(Skill.class).addAnnotatedClass(Project.class) | ||
.buildSessionFactory(new StandardServiceRegistryBuilder().build()); | ||
repository = new Repository(sessionFactory); | ||
} | ||
|
||
@AfterEach | ||
public void afterEach() { | ||
sessionFactory.close(); | ||
} | ||
|
||
@Test | ||
public void lookup() { | ||
long projectId = createData("Ben", "Caffeine", "Hibernate"); | ||
|
||
// db hit | ||
Project project = repository.getProject(projectId); | ||
|
||
// cache hit | ||
project = repository.getProject(projectId); | ||
assertThat(sessionFactory).hits(4).misses(0).puts(3); | ||
|
||
// cache hit due to project associations | ||
repository.getUser(project.getAssignee().getId()); | ||
assertThat(sessionFactory).hits(5).misses(0).puts(3); | ||
|
||
// cache hit due to only the project being explicitly evicted | ||
repository.evictProject(projectId); | ||
repository.getUser(project.getAssignee().getId()); | ||
assertThat(sessionFactory).hits(6).misses(0).puts(3); | ||
|
||
// db hit | ||
project = repository.getProject(projectId); | ||
assertThat(sessionFactory).hits(6).misses(1).puts(4); | ||
try (var sessionFactory = newSessionFactory()) { | ||
long projectId = sessionFactory.fromTransaction(session -> | ||
createData(session, "Ben", "Caffeine", "Hibernate")); | ||
|
||
// db hit | ||
var project = sessionFactory.fromSession(session -> Queries_.getProject(session, projectId)); | ||
|
||
// cache hit | ||
sessionFactory.inSession(session -> Queries_.getProject(session, projectId)); | ||
assertThat(sessionFactory).hits(4).misses(0).puts(3); | ||
|
||
// cache hit due to project associations | ||
sessionFactory.fromSession(session -> | ||
Queries_.getUser(session, project.getAssignee().getId())); | ||
assertThat(sessionFactory).hits(5).misses(0).puts(3); | ||
|
||
// cache hit due to only the project being explicitly evicted | ||
sessionFactory.getCache().evict(Project.class, projectId); | ||
sessionFactory.fromSession(session -> | ||
Queries_.getUser(session, project.getAssignee().getId())); | ||
assertThat(sessionFactory).hits(6).misses(0).puts(3); | ||
|
||
// db hit | ||
sessionFactory.inSession(session -> Queries_.getProject(session, projectId)); | ||
assertThat(sessionFactory).hits(6).misses(1).puts(4); | ||
} | ||
} | ||
|
||
@Test | ||
public void query() { | ||
long projectId = createData("Ben", "Caffeine", "Hibernate"); | ||
|
||
// db hit | ||
repository.findProject(projectId); | ||
assertThat(sessionFactory).hits(1).misses(0).puts(3); | ||
|
||
// cache hit | ||
repository.findProject(projectId); | ||
assertThat(sessionFactory).hits(2).misses(0).puts(3); | ||
|
||
// db hit | ||
repository.findProjects(); | ||
assertThat(sessionFactory).hits(3).misses(0).puts(3); | ||
|
||
// cache hit | ||
repository.findProjects(); | ||
assertThat(sessionFactory).hits(4).misses(0).puts(3); | ||
|
||
// db hit | ||
repository.evictAll(); | ||
repository.findProject(projectId); | ||
assertThat(sessionFactory).hits(5).misses(0).puts(3); | ||
|
||
// query cache hit | ||
repository.findProject(projectId); | ||
assertThat(sessionFactory).hits(6).misses(0).puts(3); | ||
|
||
// db hit | ||
repository.findProjects(); | ||
assertThat(sessionFactory).hits(7).misses(0).puts(3); | ||
|
||
// cache hit | ||
repository.findProjects(); | ||
assertThat(sessionFactory).hits(8).misses(0).puts(3); | ||
|
||
// automatically evicts project-related queries in the cache | ||
repository.updateProject(projectId, "Updated"); | ||
assertThat(sessionFactory).hits(10).misses(0).puts(4); | ||
|
||
// db hit | ||
repository.findProject(projectId); | ||
assertThat(sessionFactory).hits(11).misses(0).puts(4); | ||
try (var sessionFactory = newSessionFactory()) { | ||
long projectId = sessionFactory.fromTransaction(session -> | ||
createData(session, "Ben", "Caffeine", "Hibernate")); | ||
|
||
// db hit | ||
sessionFactory.inSession(session -> Queries_.findProject(session, projectId)); | ||
assertThat(sessionFactory).hits(1).misses(0).puts(3); | ||
|
||
// cache hit | ||
sessionFactory.inSession(session -> Queries_.findProject(session, projectId)); | ||
assertThat(sessionFactory).hits(2).misses(0).puts(3); | ||
|
||
// db hit | ||
sessionFactory.inSession(session -> Queries_.findProjects(session)); | ||
assertThat(sessionFactory).hits(3).misses(0).puts(3); | ||
|
||
// cache hit | ||
sessionFactory.inSession(session -> Queries_.findProjects(session)); | ||
assertThat(sessionFactory).hits(4).misses(0).puts(3); | ||
|
||
// db hit | ||
sessionFactory.getCache().evictDefaultQueryRegion(); | ||
sessionFactory.inSession(session -> Queries_.findProject(session, projectId)); | ||
assertThat(sessionFactory).hits(5).misses(0).puts(3); | ||
|
||
// query cache hit | ||
sessionFactory.inSession(session -> Queries_.findProject(session, projectId)); | ||
assertThat(sessionFactory).hits(6).misses(0).puts(3); | ||
|
||
// db hit | ||
sessionFactory.inSession(session -> Queries_.findProjects(session)); | ||
assertThat(sessionFactory).hits(7).misses(0).puts(3); | ||
|
||
// cache hit | ||
sessionFactory.inSession(session -> Queries_.findProjects(session)); | ||
assertThat(sessionFactory).hits(8).misses(0).puts(3); | ||
|
||
// automatically evicts project-related queries in the cache | ||
sessionFactory.inTransaction(session -> { | ||
var project = Queries_.getProject(session, projectId); | ||
project.setName("Updated"); | ||
session.merge(project); | ||
}); | ||
assertThat(sessionFactory).hits(10).misses(0).puts(4); | ||
|
||
// db hit | ||
sessionFactory.inSession(session -> Queries_.findProject(session, projectId)); | ||
assertThat(sessionFactory).hits(11).misses(0).puts(4); | ||
|
||
// db hit | ||
sessionFactory.inSession(session -> Queries_.findProjects(session)); | ||
assertThat(sessionFactory).hits(12).misses(0).puts(4); | ||
} | ||
} | ||
|
||
// db hit | ||
repository.findProjects(); | ||
assertThat(sessionFactory).hits(12).misses(0).puts(4); | ||
private static SessionFactory newSessionFactory() { | ||
return new Configuration().addAnnotatedClass(User.class) | ||
.addAnnotatedClass(Skill.class).addAnnotatedClass(Project.class) | ||
.buildSessionFactory(new StandardServiceRegistryBuilder().build()); | ||
} | ||
|
||
private long createData(String userName, String projectName, String skillName) { | ||
private long createData(Session session, String userName, String projectName, String skillName) { | ||
var project = new Project(); | ||
var skill = new Skill(); | ||
var user = new User(); | ||
|
@@ -133,7 +135,9 @@ private long createData(String userName, String projectName, String skillName) { | |
|
||
skill.setName(skillName); | ||
|
||
repository.persist(user, project, skill); | ||
session.persist(project); | ||
session.persist(skill); | ||
session.persist(user); | ||
return project.getId(); | ||
} | ||
} |
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