grails / grails-data-mapping

GORM - Groovy Object Mapping
http://gorm.grails.org/
217 stars 197 forks source link

Tenants.withoutId won't work for Entity persisting #1032

Closed kevinclcn closed 6 years ago

kevinclcn commented 6 years ago

Steps to Reproduce

  1. create an entity extended the MultiTenant traits, for example:
class Demo implements MultiTenant<Demo> {
   Integer tenantId
   String message
}
  1. save the Entity with Tenants.withoutId()
Tenants.withoutId {
   def demo = new Demo()
   demo.tenantId = 1
   demo.message = 'just a test'
   demo.save()
}

Expected Behaviour

The demo should be persisted into mysql

Actual Behaviour

Below exception is thrown:

Caused by: java.lang.IllegalArgumentException: Cannot assign value [DEFAULT] to property [tenantId] of type [java.lang.Integer] of class [demo.Demo]. The value could not be converted to the appropriate type: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'DEFAULT'; nested exception is java.lang.NumberFormatException: For input string: "DEFAULT"
    at org.grails.datastore.mapping.reflect.FieldEntityAccess.setProperty(FieldEntityAccess.java:95)
    at org.grails.orm.hibernate.multitenancy.MultiTenantEventListener.onApplicationEvent(MultiTenantEventListener.java:74)

Environment Information

other information

The Tenants.withoutId() works for Demo.get(1), so I think this should be a bug.

KoloritnijClap commented 6 years ago

If you want to save under another tenant better to use Tenants.withTenant(1) {} It will work like you expect.