Graphic mapping one-way display on a complex key

I am using grails 2.0.0. I have three Member, Product and ProductType objects. A member has many products and a one-to-many relationship. The product points to ProductType (lookup table) and is a many-to-one relationship. My question is about removing products. It works in one scenario and not in another. Read on.

A rough outline of the mappings displayed below:

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'
    ...
   }
}

I received a customer service code whose circuit ...

    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)
        }
    }

So far so good. It works great. However, when I enter the composite primary key for the T_PRODUCT table, by doing the following:

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

I get this:

org.hibernate.StaleStateException: upda [0]; : 0; : 1 org.hibernate.StaleStateException: [0]; : 0; : 1 ProductService.cleanUpGorm(ProductService.groovy: 442) ProductService.maintainProduct(ProductService.groovy: 213) ClientService $_maintainMembers_closure5.doCall(ClientService.groovy: 158)    ClientService.maintainMembers(ClientService.groovy: 152) ClientService.processMembers(ClientService.groovy: 394)

, ?

+5
2

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()
        }

    }

, .

.

+6

Grails doc, 5.5.2.5 . , :

  • , , Serializable equals() hashCode(), .
  • , - , .
  • , .

, .

+1

All Articles