hibernate - Grails组合键上的一对多映射stalestaleException

标签 hibernate grails gorm

我使用grails 2.0.0。我有三个对象Member,Product和ProductType。成员有很多产品,并且是一对多关系。产品指向ProductType(引用表),并且是多对一关系。我的问题是关于删除产品。它在一种情况下起作用,而在另一种情况下不起作用。继续阅读。

下面的映射的粗略概述:

Member.groovy:

class Member  {
   Long id
   ....
   SortedSet products
   static hasMany = [products:Product]
   static mapping = {
        table 'T_MEMBER'
        id column:'MEMBER_ID'...
       products cascade: "all-delete-orphan"
   }
}

Product.groovy:
class Product {
   Long id
   ProductType productType
   ...
   static belongsTo = [member:Member]
   static mapping = {
        table 'T_PRODUCT'
        id column:'PRODUCT_ID'
        member column: 'MEMBER_ID'
        productType column: 'PRODUCT_TYPE'
        ...
   }
}

ProductType.groovy:
class ProductType {
   Long id
   ..
   static mapping = {
        table 'T_PRODUCT_TYPE'
        id column:'PRODUCT_TYPE', generator:'assigned'
    ...
   }
}

我得到的客户服务代码的轮廓是...
    if((newMember.products) && (newMember.products.size() >0)) {
        def addList = newMember.products - existingMember.products
        def removeList = existingMember.products- newMember.products
        removeList.each { product ->
            existingMember.removeFromProducts(product)
        }
        addList.each {product ->
            existingMember.addToProducts(product)
        }
    }

到现在为止还挺好。这是完美的工作。但是,当我通过执行以下操作为T_PRODUCT表引入复合主键时:
   static mapping = {
        table 'T_PRODUCT'
        //id column:'PRODUCT_ID'
        id composite:['member', 'productType']
        member column: 'MEMBER_ID'
        productType column: 'PRODUCT_TYPE'
        ...
   }

我得到这个:

org.hibernate.StaleStateException: Batch upda Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 at ProductService.cleanUpGorm(ProductService.groovy:442) at ProductService.maintainProduct(ProductService.groovy:213) at ClientService$_maintainMembers_closure5.doCall(ClientService.groovy:158) at ClientService.maintainMembers(ClientService.groovy:152) at ClientService.processMembers(ClientService.groovy:394)



知道我哪里可能出问题了吗?

最佳答案

您的Product域类必须实现Serializable并覆盖方法hashCode()equals(),这必须在使用复合键的情况下完成。

您的产品域类必须是这样的

class Product implements Serializable {
        Long id
        ProductType productType
        ...

        static mapping = {
             table 'T_PRODUCT'
             id composite:['member', 'productType']
             member column: 'MEMBER_ID'
             productType column: 'PRODUCT_TYPE'
        }

        boolean equals(other) {
            if (!(other instanceof Product )) {
                return false
            }
            other.member== member && other.productType== productType
        }

        int hashCode() {
            def builder = new HashCodeBuilder()
            builder.append member
            builder.append productType
            builder.toHashCode()
        }

    }

我认为这样一切都会好起来的。

如有问题,请写。

关于hibernate - Grails组合键上的一对多映射stalestaleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10422980/

相关文章:

java - Hibernate:如何使用@Embeddables 的自定义集合?

grails - 如何在GORM中更改父项?

grails - 多对多Grails-查找包含特定对象列表的所有对象

grails - 如何运行多个 Groovy/Grails 版本

hibernate - Grails的hasOne和hasMany具有相同的域和级联操作

grails - 具有自定义联接表域的多对多条件

grails - Grails 2.5在服务类上设置数据源不起作用

java - hibernate validator 的问题 - javax.persistence.RollbackException (spring)

java - 组织.hibernate.id.IdentifierGenerationException : ids for this class must be manually assigned before calling save(): while id is autogenerate in oracle

java - 如何仅获取与 JPA 中其他 2 个表具有 @OneToMany 关系的父记录