Divya
Divya

Reputation: 137

How to prevent update of a record in Grails3 service method for get

I have few domain classes in our application like Customer and Tenant. Customer domain instance has tenantId reference but there is no association with Tenant domain class.

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode
class Customer {

    Integer id
    Integer tenantId
    Long version
    String type
    Date dateCreated
    Date lastUpdated

    static mapping = {
        table "customer"
        id column: "customerid", generator:'native', params:[sequence:'customer_customerid_seq']
        tenantId column:"tenantid"
        version column: "version"
        dateCreated column:"datecreated"
        lastUpdated column:"lastupdated"
    }
}

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode
class Tenant {

    Long id
    String name
    Long version

    static mapping = {
        table "tenant"
        id column: "tenantid"
    }
}

We are trying to fetch a type of Customer in OrderController. Since it is used in multiple methods I created a service method 'getClerkCustomer' to fetch the Customer. tenantId value is stored in session once the user login. So the tenantId value in OrderController is fetched from session and passed to 'getClerkCustomer'

Customer getClerkCustomer(Long tenantId) {

    Customer customer = Customer.findByTypeAndTenantId(CUSTOMER_TYPE_CLERK, Math.toIntExact(tenantId))

    return (customer)
}

When the 'getClerkCustomer' is executed it retrvies the Customer based on type and tenantId. Along with fetching the Customer record it even updates the Tenant record for that tenantId (version value is incremented). I am not sure why the Tenant record version is incremented when we fetch the Customer record. Does Grails have any cenvention to update the record by name?

Can anyone let me know why is the Tenant updated for a Customer fetch and how can I avoid that?

Note : We encounter this issue with other domain instances as well. Updating of once instance increments version on different instance. Above is one such scenario.

Version of Grails is 3.0.9
Datbase is postgres

Upvotes: 0

Views: 128

Answers (0)

Related Questions