如何解决将Micronaut + GORM与多个数据源一起使用时,如何配置applicatiuon.yml
环境:Java 11 + Micronaut 2.1 + GORM。
我有两个数据库要连接。 PostgreSQL和MSSQL。我尝试了一些在Internet上查找的方法,但都失败了。以下是我的详细配置,代码和输出日志。请指导我如何正确配置application.yml。谢谢
参考站点:
我的application.yml如下:
micronaut: application: name: demo --- hibernate: hbm2ddl: auto: none cache: queries: false use_second_level_cache: false use_query_cache: false --- dataSource: dbCreate: none url: jdbc:postgresql://localhost/test_db1 dialect: org.hibernate.dialect.PostgreSQLDialect pooled: true jmxExport: true driverClassName: org.postgresql.Driver username: 'user1' password: 'user1' dataSources: emp: pooled: true jmxExport: true dialect: org.hibernate.dialect.SQLServer2012Dialect driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver dbCreate: none url: jdbc:sqlserver://localhost:1433;databaseName=test_db2 username: 'user2' password: 'user2'
我的实体类:
import grails.gorm.annotation.Entity
import org.springframework.context.i18n.LocaleContextHolder
@Entity
class Country {
String enDisplayName;
static mapping = {
version false
}
static constraints = {
enDisplayName nullable: false
}
}
@Entity
class Employee {
String employeeId
String employeeName
static mapping = {
dataSource 'emp'
version false
id name:'employeeId',generator:'assigned'
}
}
我的主要代码:
package com.example
import com.example.domain.Country
import com.example.domain.Employee
import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters
@Command(name = 'demo',description = '...',mixinStandardHelpOptions = true)
class DemoCommand implements Runnable {
@Option(names = ['-v','--verbose'],description = '...')
boolean verbose
static void main(String[] args) throws Exception {
PicocliRunner.run(DemoCommand.class,args)
}
void run() {
// business logic here
if (verbose) {
println "Hi!"
}
Country.withNewSession {
List<Country> list = Country.list()
println("Country count:${list.size()}")
}
Employee.withNewSession {
Employee employee = Employee.findByEmployeeNo('BU4191')
println("employee name:${employee.employeeName}")
}
}
}
日志:
16:25:26.624 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [cli]
16:25:26.928 [main] INFO i.m.c.h.g.HibernateDatastoreFactory - Starting GORM for Hibernate
16:25:27.558 [main] INFO org.hibernate.Version - HHH000412: Hibernate ORM core version 5.4.21.Final
16:25:27.682 [main] INFO o.h.validator.internal.util.Version - HV000001: Hibernate Validator 6.1.5.Final
16:25:27.819 [main] INFO o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
16:25:28.646 [main] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
16:25:29.609 [main] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.SQLServer2012Dialect
Country count:4
16:25:30.253 [main] WARN org.hibernate.orm.deprecation - HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead
16:25:30.283 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0,SQLState: 42703
16:25:30.283 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ERROR: column this_.employee_id does not exist
Position: 8
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not extract ResultSet; bad SQL grammar [n/a]; nested exception is org.postgresql.util.PSQLException: ERROR: column this_.employee_id does not exist
Position: 8
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.grails.orm.hibernate.GrailsHibernateTemplate.convertJdbcAccessException(GrailsHibernateTemplate.java:725)
at org.grails.orm.hibernate.GrailsHibernateTemplate.convertHibernateAccessException(GrailsHibernateTemplate.java:712)
at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:301)
at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:241)
at org.grails.orm.hibernate.GrailsHibernateTemplate.executeWithNewSession(GrailsHibernateTemplate.java:153)
at org.grails.orm.hibernate.AbstractHibernateDatastore.withNewSession(AbstractHibernateDatastore.java:360)
at org.grails.orm.hibernate.AbstractHibernateGormStaticApi.withNewSession(AbstractHibernateGormStaticApi.groovy:78)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.withNewSession(GormEntity.groovy:1027)
at org.grails.datastore.gorm.GormEntity$Trait$Helper$withNewSession.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
at com.example.domain.Employee.withNewSession(Employee.groovy)
at com.example.domain.Employee$withNewSession.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
at com.example.DemoCommand.run(DemoCommand.groovy:34)
at picocli.CommandLine.executeUserObject(CommandLine.java:1919)
at picocli.CommandLine.access$1100(CommandLine.java:145)
at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2332)
at picocli.CommandLine$RunLast.handle(CommandLine.java:2326)
at picocli.CommandLine$RunLast.handle(CommandLine.java:2291)
at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2159)
at picocli.CommandLine.execute(CommandLine.java:2058)
at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:137)
at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:114)
at io.micronaut.configuration.picocli.PicocliRunner$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
at com.example.DemoCommand.main(DemoCommand.groovy:21)
Caused by: org.postgresql.util.PSQLException: ERROR: column this_.employee_id does not exist
Position: 8
解决方法
最终,我尝试了如下成功的结果:
micronaut:
application:
name: demo
dataSource:
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
driverClassName: org.h2.Driver
username: sa
password: ''
pooled: true
jmxExport: true
hibernate:
hbm2ddl:
auto: update
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
dataSources:
seconddb:
url: jdbc:h2:mem:devDb2;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
driverClassName: org.h2.Driver
username: sa
password: ''
readOnly: true
pooled: true
jmxExport: true
hibernate:
hbm2ddl:
auto: update
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。