java - 通过 hibernate 连接到MySQL

标签 java hibernate grails

我的数据库:

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

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

创建域
package testgrails12

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

控制者
package testgrails12

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

我在执行阶段遇到错误

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'



我试图在conf文件夹中创建hibernate.cfg.xml,Teams.hbm.xml,但这没有用。

如何正确设置一切?工作已停止=(

添加映射后出现错误

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

最佳答案

最简单的解决方案是从实体中删除Integer idteam,然后让GORM为您创建数据库架构。

如果您使用一些旧数据库,则可以使用static mapping = {}闭包(如给定here)对齐实体映射,您的id字段可能如下所示:

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

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

关于java - 通过 hibernate 连接到MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055167/

相关文章:

java - 从图像样本中获取卷积矩阵?

java - 可以用 Java 创建 Internet 过滤器吗?

java - Hibernate 的复合键

java - GWT 远程日志记录无法记录 Throwable Stacktrace?

grails - 如何将系统属性传递给Grails 3数据库迁移命令

渲染具有多对多关系的复杂表单时 hibernate 未保存的 transient 错误

java - 如果我在 OnTouchListener 中返回 false,实际会发生什么?

java - 发生 EOFException 时返回是否安全

oracle - Java Hibernate + Oracle 11.2 锁定

java - Hibernate:按日期和时间排序数据