-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
190 additions
and
47 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
75 changes: 75 additions & 0 deletions
75
...main/java/io/micronaut/data/spring/jpa/hibernate/SpringHibernateConnectionOperations.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,75 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* 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 | ||
* | ||
* https://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 io.micronaut.data.spring.jpa.hibernate; | ||
|
||
import io.micronaut.context.annotation.EachBean; | ||
import io.micronaut.context.annotation.Replaces; | ||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.data.connection.manager.ConnectionDefinition; | ||
import io.micronaut.data.connection.manager.synchronous.ConnectionOperations; | ||
import io.micronaut.data.connection.manager.synchronous.ConnectionStatus; | ||
import io.micronaut.data.connection.support.DefaultConnectionStatus; | ||
import io.micronaut.data.hibernate.connection.HibernateConnectionOperations; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.springframework.orm.hibernate5.HibernateTemplate; | ||
import org.springframework.orm.hibernate5.SessionHolder; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Spring JDBC Hibernate Session operations. | ||
* | ||
* @author Denis Stepanov | ||
* @since 4.0.0 | ||
*/ | ||
@Internal | ||
@EachBean(SessionFactory.class) | ||
@Replaces(HibernateConnectionOperations.class) | ||
// TODO: We should avoid using @Replaces, there should be a way to use different data sources with Micronaut and Spring TX | ||
public final class SpringHibernateConnectionOperations implements ConnectionOperations<Session> { | ||
|
||
private final SessionFactory sessionFactory; | ||
|
||
SpringHibernateConnectionOperations(SessionFactory sessionFactory) { | ||
this.sessionFactory = sessionFactory; | ||
} | ||
|
||
@Override | ||
public Optional<ConnectionStatus<Session>> findConnectionStatus() { | ||
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); | ||
if (sessionHolder != null && sessionHolder.getEntityManager() != null) { | ||
return Optional.of(createStatus(sessionHolder.getSession())); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public <R> R execute(ConnectionDefinition definition, Function<ConnectionStatus<Session>, R> callback) { | ||
return new HibernateTemplate(sessionFactory).execute(session -> callback.apply(createStatus(session))); | ||
} | ||
|
||
private DefaultConnectionStatus<Session> createStatus(Session session) { | ||
return new DefaultConnectionStatus<>( | ||
session, | ||
ConnectionDefinition.DEFAULT, | ||
true | ||
); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
data-spring-jpa/src/test/groovy/io/micronaut/data/spring/hibernate/H2Properties.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,21 @@ | ||
package io.micronaut.data.spring.hibernate | ||
|
||
import io.micronaut.test.support.TestPropertyProvider | ||
|
||
trait H2Properties implements TestPropertyProvider { | ||
|
||
@Override | ||
Map<String, String> getProperties() { | ||
return [ | ||
"datasources.default.name" : "mydb", | ||
"datasources.default.transactionManager" : "springHibernate", | ||
"datasources.default.schema-generate" : "create-drop", | ||
"datasources.default.dialect" : "h2", | ||
"datasources.default.driver-class-name" : "org.h2.Driver", | ||
"datasources.default.username" : "sa", | ||
"datasources.default.password" : "", | ||
"datasources.default.url" : "jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE", | ||
"jpa.default.properties.hibernate.hbm2ddl.auto": "create-drop" | ||
] | ||
} | ||
} |
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
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
Oops, something went wrong.