通过休眠连接到MySQL [英] Connect to MySQL via hibernate

查看:168
本文介绍了通过休眠连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My db:

  CREATE TABLE`ggloor`.`teams`(
`idteam` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45)NULL,
PRIMARY KEY(`idteam`));

applicatin.yml中的设置



<$ p $ >
cache:
queries:false
use_second_level_cache:true
use_query_cache:false
region.factory_class:org.hibernate.cache .ehcache.SingletonEhCacheRegionFactory
$ b $ dataSource:
pooled:true
jmxExport:true
driverClassName:com.mysql.jdbc.Driver
username:root
密码:1111

环境:
开发:
dataSource:
dbCreate:create-drop
url:jdbc:mysql:// localhost: 3306 / ggloor?useSSL = false
test:
dataSource:
dbCreate:create-drop
url:jdbc:mysql:// localhost:3306 / ggloor?autoreconnect = true; MVCC = TRUE; LOCK_TIMEOUT = 10000; DB_CLOSE_ON_EXIT = FALSE
生产:
dataSource:
dbCreate:create-drop
url:jdbc:mysql:// localhost:33 06 / ggloor?autoreconnect = true; MVCC = TRUE; LOCK_TIMEOUT = 10000; DB_CLOSE_ON_EXIT = FALSE
属性:
jmxEnabled:true
initialSize:5
maxActive:50
minIdle:5
maxIdle:25
maxWait:10000
maxAge:600000
timeBetweenEvictionRunsMillis:5000
minEvictableIdleTimeMillis:60000
validationQuery:SELECT 1
validationQueryTimeout:3
validationInterval:15000
testOnBorrow:true
testWhileIdle:true
testOnReturn:false
jdbcInterceptors:ConnectionState
defaultTransactionIsolation:2#TRANSACTION_READ_COMMITTED

创建的域名

  package testgrails12 

class团队{
In teger idteam
字符串名称
静态约束= {
}
}

Controller

  package testgrails12 
$ b $ class class {b $ b Integer idteam
字符串名称
静态约束= {
}
静态映射= {
id列:'idteam',sqlType:'INT(11)',可插入:false, updateable:false
}
}

在执行阶段出现错误 b
$ b


错误500:内部服务器错误URI / bdconnect / index类
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException消息null
由'字段列表'中未知的列'teams0_.id'导致

我试图创建hibernate.cfg。 xml,conf文件夹中的Teams.hbm.xml,这是行不通的。



如何正确设置所有内容?停止工作=(

添加映射后出错


2017-03-29 19:30:34.954错误--- [ost-startStop-1]
osbcembedded.tomcat.TomcatStarter:启动Tomcat
上下文时出错,例外:
org.springframework.beans.factory。 BeanCreationException。消息:
创建名为'grailsCacheFilter'的bean时出错:无法创建类型为
的内部
'(内部bean)#4bafa64a'[grails.plugin.cache.web.filter。 simple.MemoryPageFragmentCachingFilter]
设置bean属性'filter';嵌套的异常是
org.springframework.beans.factory.UnsatisfiedDependencyException:
创建名为'(内部bean)的bean'错误#4bafa64a' :通过方法'setUrlMappingsHandlerMapping'
参数0表示的不满意的
依赖项;嵌套的异常是
org.springframework.beans.factory.UnsatisfiedDependencyException:
创建名为'urlMappingsHandlerMa pping':通过方法'setWebRequestInterceptors'
参数0表示的不满意的
依赖项。嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'openSessionInViewInterceptor'的bean:设置bean属性
时无法解析
引用bean'hibernateDatastore' 'hibernateDatastore';嵌套异常是
org.springframework.beans.factory.BeanCreationException:错误
创建名为'hibernateDatastore'的bean:Bean实例化通过
构造函数失败;嵌套异常是
org.springframework.beans.BeanInstantiationException:失败
实例化[org.grails.orm.hibernate.HibernateDatastore]:构造函数
抛出异常;嵌套异常是org.hibernate.MappingException:
实体映射中的重复列:testgrails12.Teams列:
idteam(应该使用insert =falseupdate =false映射)
2017-03-29 19:30:35.003错误--- [main]
osboot.SpringApplication:应用程序启动失败


Integer idteam ,并让GORM为您创建数据库模式。



如果您使用一些遗留数据库,您可以使用静态映射= {} 闭包来对齐实体映射,如给定这里,你的id字段可能看起来像这样:

 静态映射= {
....
id列:'idteam',sqlType:'INT(11)' ,可插入:false,可更新:false
....
}

注意:您不需要将其定义为实体中的字段。只需在映射中使用它即可。


My db:

CREATE TABLE `ggloor`.`teams` (
  `idteam` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  PRIMARY KEY (`idteam`));

Settings in applicatin.yml

hibernate:
    cache:
        queries: false
        use_second_level_cache: true
        use_query_cache: false
        region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    username: root
    password: 1111

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://localhost:3306/ggloor?useSSL=false
    test:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://localhost:3306/ggloor?autoreconnect=true;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    production:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://localhost:3306/ggloor?autoreconnect=true;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
            properties:
                jmxEnabled: true
                initialSize: 5
                maxActive: 50
                minIdle: 5
                maxIdle: 25
                maxWait: 10000
                maxAge: 600000
                timeBetweenEvictionRunsMillis: 5000
                minEvictableIdleTimeMillis: 60000
                validationQuery: SELECT 1
                validationQueryTimeout: 3
                validationInterval: 15000
                testOnBorrow: true
                testWhileIdle: true
                testOnReturn: false
                jdbcInterceptors: ConnectionState
                defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

Created domain

package testgrails12

class Teams {
    Integer idteam
    String name
    static constraints = {
    }
}

Controller

package testgrails12

class Teams {
    Integer idteam
    String name
    static constraints = {
    }
    static mapping = {
        id column: 'idteam', sqlType: 'INT(11)', insertable: false, updateable: false
    }
}

I get an error at the execution stage

Error 500: Internal Server Error URI /bdconnect/index Class com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException Message null Caused by Unknown column 'teams0_.id' in 'field list'

I tried to create hibernate.cfg.xml, Teams.hbm.xml in the conf folder, this did not work.

How to set everything up correctly? Work stopped =(

Error after add mapping

2017-03-29 19:30:34.954 ERROR --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'grailsCacheFilter': Cannot create inner bean '(inner bean)#4bafa64a' of type [grails.plugin.cache.web.filter.simple.MemoryPageFragmentCachingFilter] while setting bean property 'filter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#4bafa64a': Unsatisfied dependency expressed through method 'setUrlMappingsHandlerMapping' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'urlMappingsHandlerMapping': Unsatisfied dependency expressed through method 'setWebRequestInterceptors' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openSessionInViewInterceptor': Cannot resolve reference to bean 'hibernateDatastore' while setting bean property 'hibernateDatastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: testgrails12.Teams column: idteam (should be mapped with insert="false" update="false") 2017-03-29 19:30:35.003 ERROR --- [ main] o.s.boot.SpringApplication : Application startup failed

解决方案

The easiest solution is to remove Integer idteam from your entity and let GORM create the DB schema for you.

If you use some legacy database you can align your entity mapping using the static mapping = {} closure as given here, your id field probably will look like that:

static mapping = {
    ....
    id column: 'idteam', sqlType: 'INT(11)', insertable: false, updateable: false
    ....
}

Note: you do not need to define it as a field in your entity. Just use it in mapping.

这篇关于通过休眠连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆