forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hibernate StatelessSession support
Closes: quarkusio#7148
- Loading branch information
Showing
11 changed files
with
1,207 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
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
46 changes: 46 additions & 0 deletions
46
.../test/java/io/quarkus/hibernate/orm/stateless/StatelessSessionWithinRequestScopeTest.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,46 @@ | ||
package io.quarkus.hibernate.orm.stateless; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.hibernate.StatelessSession; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.hibernate.orm.MyEntity; | ||
import io.quarkus.hibernate.orm.naming.PrefixPhysicalNamingStrategy; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StatelessSessionWithinRequestScopeTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyEntity.class, PrefixPhysicalNamingStrategy.class) | ||
.addAsResource(EmptyAsset.INSTANCE, "import.sql") | ||
.addAsResource("application-physical-naming-strategy.properties", "application.properties")); | ||
|
||
@Inject | ||
StatelessSession statelessSession; | ||
|
||
@BeforeEach | ||
public void activateRequestContext() { | ||
Arc.container().requestContext().activate(); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
Number result = (Number) statelessSession.createNativeQuery("SELECT COUNT(*) FROM TBL_MYENTITY").getSingleResult(); | ||
assertEquals(0, result.intValue()); | ||
} | ||
|
||
@AfterEach | ||
public void terminateRequestContext() { | ||
Arc.container().requestContext().terminate(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...c/test/java/io/quarkus/hibernate/orm/stateless/StatelessSessionWithinTransactionTest.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,47 @@ | ||
package io.quarkus.hibernate.orm.stateless; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.transaction.UserTransaction; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.StatelessSession; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.enhancer.Address; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StatelessSessionWithinTransactionTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClass(Address.class)) | ||
.withConfigurationResource("application.properties"); | ||
|
||
@Inject | ||
StatelessSession statelessSession; | ||
|
||
@Inject | ||
Session session; | ||
|
||
@Inject | ||
UserTransaction transaction; | ||
|
||
@Test | ||
public void test() throws Exception { | ||
transaction.begin(); | ||
Address entity = new Address("high street"); | ||
session.persist(entity); | ||
transaction.commit(); | ||
|
||
transaction.begin(); | ||
List<Object> list = statelessSession.createQuery("SELECT street from Address").getResultList(); | ||
assertThat(list).containsOnly("high street"); | ||
transaction.commit(); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...e/src/main/java/io/quarkus/hibernate/orm/runtime/RequestScopedStatelessSessionHolder.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,31 @@ | ||
package io.quarkus.hibernate.orm.runtime; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.annotation.PreDestroy; | ||
import jakarta.enterprise.context.RequestScoped; | ||
|
||
import org.hibernate.SessionFactory; | ||
import org.hibernate.StatelessSession; | ||
|
||
/** | ||
* Bean that is used to manage request scoped stateless sessions | ||
*/ | ||
@RequestScoped | ||
public class RequestScopedStatelessSessionHolder { | ||
|
||
private final Map<String, StatelessSession> sessions = new HashMap<>(); | ||
|
||
public StatelessSession getOrCreateSession(String name, SessionFactory factory) { | ||
return sessions.computeIfAbsent(name, (n) -> factory.openStatelessSession()); | ||
} | ||
|
||
@PreDestroy | ||
public void destroy() { | ||
for (Map.Entry<String, StatelessSession> entry : sessions.entrySet()) { | ||
entry.getValue().close(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.