Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6a4aa88

Browse files
authored
Merge pull request #955 from grails/exclude-hibernate-core
Exclude hibernate-ehcache
2 parents 8c0dd17 + eecb602 commit 6a4aa88

File tree

6 files changed

+51
-65
lines changed

6 files changed

+51
-65
lines changed

docs/src/docs/asciidoc/gettingStarted/springBoot.adoc

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,49 @@
1-
To use GORM for Hibernate in Spring Boot add the necessary dependencies to your Boot application:
1+
To use GORM for Hibernate in Spring Boot, add the necessary dependency to your Boot application:
22

33
[source,groovy,subs="attributes"]
4+
.build.gradle
45
----
5-
compile("org.grails:gorm-hibernate5-spring-boot:{version}")
6-
compile "org.hibernate:hibernate-core-jakarta"
7-
compile "org.hibernate:hibernate-ehcache"
8-
runtime "com.h2database:h2:1.4.192"
9-
// for MySQL
10-
// runtime "mysql:mysql-connector-java"
11-
12-
// for connection pooling
13-
runtime "org.apache.tomcat:tomcat-jdbc:8.5.0"
14-
runtime "org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.5.0"
6+
implementation 'org.grails:gorm-hibernate5-spring-boot:{version}'
157
----
168

17-
Then ensure you have configured a https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html[datasource and Hibernate as per the Spring Boot guide]. For example in the case of MySQL:
9+
Then ensure you have configured a https://docs.spring.io/spring-boot/reference/data/sql.html#data.sql.datasource[datasource and Hibernate as per the Spring Boot guide]. For example in the case of MySQL:
1810

1911
[source,yaml]
12+
.application.yml
2013
----
21-
hibernate:
22-
hbm2ddl:
23-
auto: update
24-
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
25-
spring:
26-
datasource:
27-
driverClassName: com.mysql.jdbc.Driver
28-
url: jdbc:mysql://127.0.0.1:3306/gorm
29-
username: root
30-
password: ""
14+
hibernate.hbm2ddl.auto: update
15+
spring.datasource.url: jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
3116
----
3217

33-
TIP: If if you prefer to use the Grails way of configuring the `DataSource` (with `dataSource.url` etc.) then you can add `@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration)` to your `Application` class, which will allow GORM to take over configuring the data source.
34-
35-
Ensure your Boot `Application` class is annotated with `ComponentScan`, for example:
18+
TIP: If you prefer to use the Grails way of configuring the `DataSource` (with `dataSource.url` etc.), these will
19+
work as well.
3620

3721
[source,groovy]
22+
.Application.groovy
3823
----
24+
import groovy.transform.CompileStatic
3925
import org.springframework.boot.SpringApplication
40-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
41-
import org.springframework.context.annotation.*
26+
import org.springframework.boot.autoconfigure.SpringBootApplication
27+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
4228
43-
@Configuration
44-
@EnableAutoConfiguration
45-
@ComponentScan
29+
@CompileStatic
30+
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration)
4631
class Application {
4732
static void main(String[] args) {
48-
SpringApplication.run Application, args
33+
SpringApplication.run(Application, args)
4934
}
5035
}
5136
----
5237

53-
NOTE: Using `ComponentScan` without a value results in Boot scanning for classes in the same package or any package nested within the `Application` class package.
54-
If your GORM entities are in a different package specify the package name as the value of the `ComponentScan` annotation.
38+
NOTE: You need to exclude the `HibernateJpaAutoconfiguration` as we are using GORM. Using `SpringBootApplication` without a `basePackages` attribute results in Boot scanning for classes in the same package or any package nested within the `Application` class package.
39+
If your GORM entities are in a different package, specify the package name as the value of the `basePackages` attribute on the `@SpringBootApplication` annotation.
5540

5641
Finally create your GORM entities and ensure they are annotated with `grails.persistence.Entity`:
5742

5843
[source,groovy]
44+
.Person.groovy
5945
----
60-
import grails.persistence.*
46+
import grails.persistence.Entity
6147
6248
@Entity
6349
class Person {
@@ -69,6 +55,7 @@ class Person {
6955
Note that Spring Boot does not include any kind of OpenSessionInView interceptor so if you try and invoke GORM methods in a Spring `@Controller` you may encounter a session not found error. To eliminate this problem make sure your `@Controller` methods are annotated with `@Transactional`. For example:
7056

7157
[source,groovy]
58+
.PersonController.groovy
7259
----
7360
import org.springframework.transaction.annotation.Transactional
7461
import org.springframework.web.bind.annotation.RequestMapping
@@ -91,8 +78,9 @@ class PersonController {
9178
In addition, if you wish to return a GORM instance from a Spring `@Controller`, it should be noted that Spring uses Jackson for JSON marshalling, and Jackson will attempt to marshal the entire object to JSON, which can present an issue since GORM adds additional persistence related properties to your domain instance. To resolve this issue you should use `@JsonIgnoreProperties` on your GORM entity class to ignore any properties added by GORM:
9279

9380
[source,groovy]
81+
.Person.groovy
9482
----
95-
import grails.persistence.*
83+
import grails.persistence.Entity
9684
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
9785
9886
@Entity

examples/grails-hibernate/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ dependencies {
2626
runtimeOnly 'org.grails:grails-plugin-services'
2727
runtimeOnly 'org.grails:grails-plugin-url-mappings'
2828
runtimeOnly 'org.grails.plugins:fields'
29+
runtimeOnly "org.hibernate:hibernate-ehcache:$hibernateVersion", {
30+
// exclude javax variant of hibernate-core 5.6
31+
exclude group: 'org.hibernate', module: 'hibernate-core'
32+
}
33+
runtimeOnly "org.jboss.spec.javax.transaction:jboss-transaction-api_1.3_spec:$jbossTransactionApiVersion", {
34+
// required for hibernate-ehcache to work with javax variant of hibernate-core excluded
35+
}
2936
runtimeOnly 'org.springframework.boot:spring-boot-autoconfigure'
3037
runtimeOnly 'org.springframework.boot:spring-boot-starter-logging'
3138
runtimeOnly 'org.springframework.boot:spring-boot-starter-tomcat'
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
hibernate:
2-
cache:
3-
queries: false
4-
use_second_level_cache: false
5-
use_query_cache: false
6-
dataSource:
7-
pooled: true
8-
driverClassName: org.h2.Driver
9-
dbCreate: create-drop
10-
url: jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
1+
hibernate.hbm2ddl.auto: create-drop
2+
spring.dataSource.url: jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ grailsGradlePluginVersion=7.0.0-M2
66
grailsVersion=7.0.0-M1
77
groovyVersion=4.0.24
88
hibernateVersion=5.6.15.Final
9+
jbossTransactionApiVersion=2.0.0.Final
910
yakworksHibernateGroovyProxyVersion=1.1
1011
micronautPlatformVersion=4.6.3
1112
picocliVersion=4.7.6

grails-datastore-gorm-hibernate/build.gradle

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@ dependencies {
2121
exclude group:'org.slf4j', module:'slf4j-api'
2222
}
2323

24-
compileOnly "org.hibernate:hibernate-ehcache:$hibernateVersion", {
25-
exclude group:'commons-collections', module:'commons-collections'
26-
exclude group:'commons-logging', module:'commons-logging'
27-
exclude group:'com.h2database', module:'h2'
28-
exclude group:'net.sf.ehcache', module:'ehcache'
29-
exclude group:'net.sf.ehcache', module:'ehcache-core'
30-
exclude group:'org.hibernate', module:'hibernate-core'
31-
exclude group:'org.slf4j', module:'jcl-over-slf4j'
32-
exclude group:'org.slf4j', module:'slf4j-api'
33-
exclude group:'org.slf4j', module:'slf4j-log4j12'
34-
exclude group:'xml-apis', module:'xml-apis'
35-
}
36-
3724
testImplementation "org.apache.groovy:groovy-test-junit5"
3825
testImplementation "org.apache.groovy:groovy-sql"
3926
testImplementation "org.apache.groovy:groovy-json"
@@ -42,20 +29,23 @@ dependencies {
4229
}
4330
testImplementation "com.h2database:h2"
4431

45-
testImplementation "org.hibernate:hibernate-ehcache:$hibernateVersion"
46-
4732
// groovy proxy fixes bytebuddy to be a bit smarter when it comes to groovy metaClass
4833
testImplementation "org.yakworks:hibernate-groovy-proxy:$yakworksHibernateGroovyProxyVersion", {
4934
exclude group: "org.codehaus.groovy", module: "groovy"
5035
}
5136

5237
testImplementation "org.apache.tomcat:tomcat-jdbc"
53-
testRuntimeOnly "org.springframework:spring-aop"
5438

39+
testRuntimeOnly "org.hibernate:hibernate-ehcache:$hibernateVersion", {
40+
// exclude javax variant of hibernate-core 5.6
41+
exclude group: 'org.hibernate', module: 'hibernate-core'
42+
}
43+
testRuntimeOnly "org.jboss.spec.javax.transaction:jboss-transaction-api_1.3_spec:$jbossTransactionApiVersion", {
44+
// required for hibernate-ehcache to work with javax variant of hibernate-core excluded
45+
}
5546
testRuntimeOnly "org.slf4j:slf4j-simple"
5647
testRuntimeOnly "org.slf4j:jcl-over-slf4j"
57-
// The groovydoc task needs the Hibernate 4.x jars in the classpath
58-
documentation "org.hibernate:hibernate-core-jakarta:${hibernateVersion}"
48+
testRuntimeOnly "org.springframework:spring-aop"
5949
}
6050

6151
test {

grails-plugin/build.gradle

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ dependencies {
2525
api "org.springframework.boot:spring-boot"
2626
api "org.springframework:spring-orm"
2727
api "org.hibernate:hibernate-core-jakarta:$hibernateVersion"
28-
api "org.hibernate:hibernate-ehcache:$hibernateVersion"
2928
api "org.grails:grails-datastore-web"
3029
api "org.grails:grails-datastore-gorm-support"
3130
api project(":grails-datastore-gorm-hibernate5"), {
@@ -38,12 +37,21 @@ dependencies {
3837
exclude group:'org.grails', module:'grails-core'
3938
exclude group:'javax.transaction', module:'jta'
4039
}
41-
testRuntimeOnly "org.yaml:snakeyaml"
40+
4241
testImplementation "org.grails:grails-gorm-testing-support"
42+
4343
testRuntimeOnly "com.h2database:h2"
4444
testRuntimeOnly "org.apache.tomcat:tomcat-jdbc"
45+
testRuntimeOnly "org.hibernate:hibernate-ehcache:$hibernateVersion", {
46+
// exclude javax variant of hibernate-core 5.6
47+
exclude group: 'org.hibernate', module: 'hibernate-core'
48+
}
49+
testRuntimeOnly "org.jboss.spec.javax.transaction:jboss-transaction-api_1.3_spec:$jbossTransactionApiVersion", {
50+
// required for hibernate-ehcache to work with javax variant of hibernate-core excluded
51+
}
4552
testRuntimeOnly "org.springframework:spring-aop"
4653
testRuntimeOnly "org.springframework:spring-expression"
54+
testRuntimeOnly "org.yaml:snakeyaml"
4755
}
4856

4957
groovydoc.classpath += configurations.documentation

0 commit comments

Comments
 (0)