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

Commit 56964cf

Browse files
committed
docs: update GORM in Spring Boot section
- Revise the instructions for using GORM in a Spring Boot application. - Simplify the `application.yml` file in the Spring Boot test project.
1 parent 285efe8 commit 56964cf

File tree

2 files changed

+25
-47
lines changed

2 files changed

+25
-47
lines changed

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

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +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-
exclude group:'org.hibernate', module:'hibernate-core'
9-
}
10-
runtime "com.h2database:h2:1.4.192"
11-
// for MySQL
12-
// runtime "mysql:mysql-connector-java"
13-
14-
// for connection pooling
15-
runtime "org.apache.tomcat:tomcat-jdbc:8.5.0"
16-
runtime "org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.5.0"
6+
implementation 'org.grails:gorm-hibernate5-spring-boot:{version}'
177
----
188

19-
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:
2010

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

35-
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.
36-
37-
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.
3820

3921
[source,groovy]
22+
.Application.groovy
4023
----
24+
import groovy.transform.CompileStatic
4125
import org.springframework.boot.SpringApplication
42-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
43-
import org.springframework.context.annotation.*
26+
import org.springframework.boot.autoconfigure.SpringBootApplication
27+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
4428
45-
@Configuration
46-
@EnableAutoConfiguration
47-
@ComponentScan
29+
@CompileStatic
30+
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration)
4831
class Application {
4932
static void main(String[] args) {
50-
SpringApplication.run Application, args
33+
SpringApplication.run(Application, args)
5134
}
5235
}
5336
----
5437

55-
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.
56-
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.
5740

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

6043
[source,groovy]
44+
.Person.groovy
6145
----
62-
import grails.persistence.*
46+
import grails.persistence.Entity
6347
6448
@Entity
6549
class Person {
@@ -71,6 +55,7 @@ class Person {
7155
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:
7256

7357
[source,groovy]
58+
.PersonController.groovy
7459
----
7560
import org.springframework.transaction.annotation.Transactional
7661
import org.springframework.web.bind.annotation.RequestMapping
@@ -93,8 +78,9 @@ class PersonController {
9378
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:
9479

9580
[source,groovy]
81+
.Person.groovy
9682
----
97-
import grails.persistence.*
83+
import grails.persistence.Entity
9884
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
9985
10086
@Entity
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

0 commit comments

Comments
 (0)