我按照本指南http://guides.grails.org/gorm-without-grails/guide/index.html使用GORM创建了Gradle托管的Spring Boot Groovy应用程序,如果我按照指南中的说明进行操作,则可以正常工作,但是当我替换H2数据库PostgreSQL(我将使用的方式)时在实际情况下),我会相应地进行一些调整,但这根本行不通。
它打印此 stacktrace :
2018-11-02 02:26:53.840 WARN 12130 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
2018-11-02 02:26:53.840 ERROR 12130 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "hibernate_sequence" does not exist
Position: 17
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not extract ResultSet; bad SQL grammar [n/a]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
Position: 17
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(
SQLErrorCodeSQLExceptionTranslator.java:230)
... 182 more
在执行过程中,它不会在实际的postgres数据库上创建数据库模型,我想这就是为什么它说类似 关系不存在 的内容,数据库确实存在,只是它不包含表定义。
这是我的build.gradle
buildscript {
ext {
springBootVersion = '1.5.17.RELEASE'
gormVersion = '6.1.6.RELEASE'
hibernateVersion = '5.1.0.Final'
tomcatJdbcVersion = '8.5.0'
}
...
}
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
...
dependencies {
// == Groovy ==
compile 'org.codehaus.groovy:groovy'
// == Spring ==
// Web
compile 'org.springframework.boot:spring-boot-starter-web'
// = Test =
testCompile 'org.springframework.boot:spring-boot-starter-test'
// == PostgreSQL ==
runtime 'org.postgresql:postgresql'
// == GORM Hibernate 5 ==
compile "org.grails:gorm-hibernate5-spring-boot:${gormVersion}"
// Hibernate
compile "org.hibernate:hibernate-core:${hibernateVersion}"
compile "org.hibernate:hibernate-ehcache:${hibernateVersion}"
// Connection Pooling
runtime "org.apache.tomcat:tomcat-jdbc:${tomcatJdbcVersion}"
}
application.properties
文件
## Datasource
spring.datasource.url=jdbc:postgresql://localhost:5432/dirasat_api_db
spring.datasource.username=***
spring.datasource.password=***
## Hibernate Settings
# Hibernate DDL auto (create, create-drop, validate, update)
spring.jpa.database=postgresql
spring.jpa.database-platform=postgres
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jooq.sql-dialect=postgres
## Flyway configuration
flyway.enabled=false
flyway.baseline-version=1
flyway.baseline-on-migrate=true
flyway.url=${spring.datasource.url}
flyway.user=${spring.datasource.username}
flyway.password=${spring.datasource.password}
flyway.schemas=public
flyway.locations=classpath:db/migrations
flyway.validate-on-migrate=true
这是我的模型定义
Manufacturer.groovy
package com.example.demo.domain
import grails.gorm.annotation.Entity
import groovy.transform.ToString
import org.grails.datastore.gorm.GormEntity
@ToString
@Entity
class Manufacturer implements GormEntity<Manufacturer> {
String name
static hasMany = [vehicles: Vehicle]
}
Vehicle.groovy
package com.example.demo.domain
import grails.gorm.annotation.Entity
import groovy.transform.ToString
import org.grails.datastore.gorm.GormEntity
@ToString
@Entity
class Vehicle implements GormEntity<Vehicle> {
String name
Integer year
static belongsTo = [manufacturer: Manufacturer]
}
当我以测试或实际的控制器方法运行此代码时,会引发异常
@Transactional
void something() {
Manufacturer audi = new Manufacturer(name: 'audi')
audi.addToVehicles(new Vehicle(name: 'A3', year: 1996))
audi.addToVehicles(new Vehicle(name: 'A4', year: 1994))
audi.save()
Manufacturer ford = new Manufacturer(name: 'ford')
ford.addToVehicles(new Vehicle(name: 'Ford KA', year: 1996))
ford.save()
List<Manufacturer> manufacturers = Manufacturer.findAll()
List<Vehicle> vehicles = Vehicle.findAll()
for (Vehicle vehicle : vehicles)
println vehicle
for (Manufacturer manufacturer : manufacturers)
println manufacturer
}
预先感谢