标签: grails-orm

Grails和Hibernate的惰性初始化异常

org.hibernate.LazyInitializationException在Grails中最常见的地方在哪里,原因是什么,你是如何解决的?

我认为这个例外对于新手来说很多,所以如果你提供更多的例子,它会很棒.

grails hibernate grails-orm lazy-initialization

9
推荐指数
1
解决办法
7249
查看次数

如何使交易在Grails中发挥作用

摘要 父母可以有很多孩子.如何编写服务,如果在添加父项后添加子项时出错,则会回滚整个事务.例如,添加父p1,成功添加子c1,然后在添加子c2时发生错误,应回滚p1和c1.

详细问题

在以下代码中,对子项的name属性有一个唯一约束.因此,如果您尝试使用不同的父级添加相同的名称两次,则不应添加子记录,并且应回滚父记录.

我的问题是父记录没有被回滚.

我正在使用带有Grails 1.2-M2和Tomcat 6.018的带有InnoDB的MySQL.

数据源

import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration
dataSource {
    configClass = GrailsAnnotationConfiguration.class
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    dialect = org.hibernate.dialect.MySQLInnoDBDialect
    zeroDateTimeBehavior="convertToNull" //Java can't convert ''0000-00-00 00:00:00' to TIMESTAMP
    username = "root"
    password = "12345"
    loggingSql=false
}

hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
                url = "jdbc:mysql://localhost:3306/transtest?zeroDateTimeBehavior=convertToNull"

        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost:3306/transtest?zeroDateTimeBehavior=convertToNull" …
Run Code Online (Sandbox Code Playgroud)

mysql grails hibernate transactions grails-orm

9
推荐指数
1
解决办法
6089
查看次数

测试Oracle to_date函数

我正在使用GORM在Grails中编写集成测试.

我想做类似以下的事情:

delete from Statistic 
where stat_date = TO_DATE(:month_year, 'MON-YYYY')
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

java.sql.SQLException:意外的标记:语句中的TO_DATE [从统计信息中删除stat_date = TO_DATE(?,'MON-YYYY')]

我认为错误是由GORM使用的内存数据库(是H2吗?)不支持to_date函数引起的.

有关如何编写删除SQL的任何想法,以便它在测试和实时工作?

因为我只关心月份和年份,我认为我将删除stat_date在给定月份的第一个和最后一个日期之间的记录.

任何人都可以想到更好的方法吗?

testing oracle grails grails-orm

9
推荐指数
4
解决办法
8223
查看次数

ORA-00972:标识符太长 - 在Grails中避免使用它的最佳策略

保存域类对象时,我收到"ORA-00972:标识符太长"错误.

Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.intelligrape.model.Address.studentsForPermanentAddressId#79366215]
Run Code Online (Sandbox Code Playgroud)

除了减少studentsForPermanentAddressId字段的长度之外,可能有什么解决方案可以解决这个问题.原因是,这是一个我无法改变的遗留数据库表.

编辑:添加了Rob Hruska提出的域类描述

package com.intelligrape.model

class Address {

    String address1
    String address2
    String boxNumber
    String city
    Long stateLid
    String province
    String zipCode
    Long countryLid
    Double latitude
    Double longitude
    Long radius

    static hasMany = [studentsForPermanentAddressId: Student, studentsForLocalAddressId: Student]

static constraints = {
        address1 nullable: true
        address2 nullable: true
        boxNumber nullable: true, size: 1..25
        city nullable: true, size: 1..30
        stateLid nullable: true
        province nullable: true, size: 1..64
        zipCode nullable: true, size: 1..15 …
Run Code Online (Sandbox Code Playgroud)

oracle grails hibernate grails-orm

9
推荐指数
1
解决办法
9475
查看次数

使用gorm映射现有的mysql数据库

我正在尝试使用GORM和Grails 2.0映射Joomla MySQL数据库(的一小部分).

我正在阅读一本关于论证的书(Grails)并在网上搜索技术文章,但我仍然需要一个很好的参考来将Groovy/Java类型映射到MySQL字段.

我从一张简单的桌子开始jos_bannerclient.

class BannerClient {
    String name
    String contact
    String email
    String notes
    String editor = ''

    static constraints = {
        name(blank:false)
        contact(nullable:true)
        email(nullable:true)
        notes(nullable:true)
        editor(nullable:true)
    }

    static mapping = {
        datasource 'joomla'
        table 'jos_bannerclient'
        id column:'cid', type:'int'
        notes column:'extrainfo', type:'text'
        version false
    }
}
Run Code Online (Sandbox Code Playgroud)

此时记录是在数据库中生成的,但如果我保存域,failOnError:true我会收到此错误:java.lang.IllegalArgumentException.

我在映射checked_out TINYINT字段时遇到了问题.GORM验证该字段的唯一方法是将其声明为Boolean,为什么它不起作用Byte

我对如何映射MySQL TIME字段也有一些疑问checked_out_time.

我还阅读了Hibernate文档的一些部分,但仍然没有获得完成此任务所需的知识!

有人可以帮忙吗?

mysql joomla grails map grails-orm

9
推荐指数
1
解决办法
1959
查看次数

Grails:当我不想保存任何内容时,我可以使用域对象吗?

我的一些域类变得相当丰富:它们实现了一个有趣的可比较,可能有加号,减号,乘法和div,许多都有一些方便的getter来调用服务并确定复杂的东西.最重要的是,他们拥有合适的属性.我正在将这两者用于正常的"数据库事务",并且在我只想要一个具有所有这些方法但可能不想保存它的对象的时候.

我的队友确信这非常糟糕,并建议我应该使用DTO(数据传输对象),根据我的理解,这将是一个POGO/POJO,其中一个域类'代码复制/粘贴.这似乎真的不干,我看不出有什么好处.将域对象不时用作常规对象有什么问题吗?我错过了DTO的观点吗?

oop grails pojo dto grails-orm

9
推荐指数
1
解决办法
2868
查看次数

Grails findAll有sort,order,max和offset吗?

我想在findAll查询中集成sort,order,max和offset.以下工作正常:

def books = Book.findAll("from Book as b where b.approved=true order by b.dateCreated  desc", [max: max, offset: offset])
Run Code Online (Sandbox Code Playgroud)

但我想要的是:

def books = Book.findAll("from Book as b where b.approved=true", [sort: 'dateCreated', order: 'desc', max: max, offset: offset])
Run Code Online (Sandbox Code Playgroud)

这不起作用.我该如何重写呢?

grails groovy grails-orm

9
推荐指数
3
解决办法
2万
查看次数

StaleObjectStateException:Row是否被另一个事务更新或删除?

我做以下事情:

def currentUser = springSecurityService.currentUser
currentUser.name = "test"
currentUser.save(flush: true)

// some other code

currentUser.gender = "male"
currentUser.save(flush: true)        // Exception occurs
Run Code Online (Sandbox Code Playgroud)

这是我得到的例外:

ERROR events.PatchedDefaultFlushEventListener  - Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
Run Code Online (Sandbox Code Playgroud)

我该如何防止此错误?什么是最好的解决方案?

我找到了不同的方法:

  1. 在这里你可以使用discard()
  2. 在这里你可以使用merge()

我应该使用哪一个?

grails spring hibernate grails-orm grails-2.0

9
推荐指数
1
解决办法
3万
查看次数

跨两个数据源的Grails GORM域关联

belongsTo如果其他域类使用不同的数据源,是否可以在两个域类之间建立关联(即)?这两个数据源也是不同的数据库驱动程序.

我怀疑这可能是不可能的,但我想在这里与社区联系,看看是否有可能.现在我正在尝试这样做,我得到了通常怀疑的Hibernate错误:

Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table domain_class_A refers to an unmapped class: DomainClassB

样品:

class DomainClassA {
    static belongsTo = [dcB: DomainClassB]

    static mapping = {
        datasource "ds1"
        table name: "domain_class_A", schema: "schema_A"
    }
}

class DomainClassB {
    static hasMany = [dcA: DomainClassA]

    static mapping = {
        datasource "ds2"
        table name: "domain_class_B", schema: "schema_B"
    }
}
Run Code Online (Sandbox Code Playgroud)

grails datasource grails-orm relationship

9
推荐指数
1
解决办法
4204
查看次数

Grails/GORM:lazy:false和fetchMode之间的区别

在Grails/GORM中,static mapping = {xyz lazy: false}&之间有什么区别static fetchMode = [xyz: 'eager']

例:

class Book {
    static belongsTo = [author: Author]
    static mapping   = {author  lazy: false}
    static fetchMode = [author: 'eager']
}
Run Code Online (Sandbox Code Playgroud)

grails lazy-loading grails-orm fetch eager-loading

9
推荐指数
1
解决办法
3282
查看次数