我在 grails 中工作,我注意到一些我想修复的奇怪行为,但我无法弄清楚我错过了什么。我有一个可以有可能建筑物的位置。每栋建筑可以有多个套房。此外,我有一个租户,他有一个租户位置。租户位置更多地用于历史报告,以便我们可以查看租户是否移动了位置。类看起来像这样
class Location {
String name
String shortName
Country country
LocationTypeEnum locationType = LocationTypeEnum.STUD // as default
static hasMany = [building: Building]
Date dateCreated
Date lastUpdated
static constraints = {
name unique: true, maxSize: 30
shortName unique: true, maxSize: 3
country nullable: false
locationType nullable: false
}
static mapping = {
cache usage: 'nonstrict-read-write', include: 'non-lazy'
}
}
class Building {
String type
String name
Date dateCreated
Date lastUpdated
static belongsTo = [location: Location]
static hasMany = [suites: Suite]
static constraints = {
name unique: true, maxSize: 25
type inList: ["A", "B", "C"], nullable: true
}
String toString() {
name
}
}
class Suite {
int suiteNumber
Date dateCreated
static belongsTo = [building: Building]
static constraints = {
pen(validator: { return it > 0 && (it.toString().length()) <= 3 })
}
String toString() {
suite.toString()
}
}
class Tenant {
static hasMany = [tenantLocation: TenantLocation]
----
Suite suite
String comments
Date dateCreated
Date lastUpdated
boolean active = true
static constraints = {
--------
type
nullable: false
comments maxSize: 5000
}
}
class TenantLocation {
static belongsTo = [tenant: Tenant]
Tenant tenant
Suite suite
Date dateCreated
}
因此,想法是在创建租户时创建租户位置,并且仅当当前租户套件发生更改时才创建新的租户位置。
但是,我看到的不仅是tenantLocation 被保存(这是我想要的),套件也在被更新(这不是我想要的)。
例如,我有 1 号楼和 1-20 号套房,2 号楼有 1-25 号套房。我有一个租户在 1 号楼的 5 号套房,他们搬到 2 号楼的 23 号套房。现在突然 2 号楼有两个套房套房数为 5。
当我只想要租户必须更改的套房时,如何防止我的套房从一栋建筑移动到另一栋建筑?
进行更新的代码位于租户控制器中,如下所示:
def update() {
def tenantInstance = Tenant.get(params.id)
if (!tenantInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'tenant.label', default: 'Tenant'),
params.id
])
redirect action: "list"
return
}
if (params.version) {
def version = params.version.toLong()
if (tenantInstance.version > version) {
tenantInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[
message(code: 'tenantInstance.label', default: 'Tenant')] as Object[],
"Another user has updated this Tenant while you were editing")
render view: "edit", model: [tenantInstance: tenantInstance, BuidingListInstance: Building.list()]
return
}
}
tenantInstance.properties = params
if (!tenantInstance.save(flush: true)) {
render view: "edit", model: [tenantInstance: tenantInstance, BuildingListInstance: Building.list()]
return
}
def tenantLocation= TenantLocation.findAllByTenant(tenantInstance)
def locationCheck = tenantLocation.last()
//if tenant location is not null and the suite/building change create new Tenant Location entry
if (tenantLocation!=null)
{
if(locationCheck.pen.id !=tenantInstance.suite.id)
{
def location = new TenantLocation(
tenant: tenantInstance,
suite: tenantInstance.suite,
dateCreated: new Date()
).save(flush:true)
}
}
flash.message = message(code: 'default.updated.message', args: [
message(code: 'tenant.label', default: 'Tenant'),
tenantInstance.id
])
redirect action: "show", id: tenantInstance.id
}