如何在Groovy中使用java.util.Date作为映射键

时间:2014-03-27 08:47:16

标签: java grails collections groovy maps

这是我的问题:我正在尝试填写以下地图:

class Driver extends User{


final UserRole role = UserRole.DRIVER
Map<Date, Route> routes = [:]

static hasMany = [
    assignedJobs:Job
]

static mappedBy = [
    assignedJobs:'assignedUser'
]
static constraints = {
}
}

以下是填写代码的代码:

def private addStationToRoute(driver, station){
    def route = driver.routes.get(station?.estimatedArrival.clearTime())
    if(route == null)
        route = new Route()
//      route.stations.add(station)
//      route.stations.sort({it.position})
    def key = station?.estimatedArrival.clearTime()
    driver.routes.put(key, route)
}

引导我进入ClassCastException:

java.util.Date cannot be cast to java.lang.String. Stacktrace follows:
Message: java.util.Date cannot be cast to java.lang.String
Line | Method
->>   97 | $tt__save in elektrova.JobController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    200 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
经过一些研究后,我发现把钥匙放在paranthesis应该可以解决问题 (来源: http://groovy.codehaus.org/Collections地图部分 http://mrhaki.blogspot.de/2009/11/groovy-goodness-complex-keys-in-maps.html

引导我尝试这个:

def private addStationToRoute(driver, station){
    def route = driver.routes.get(station?.estimatedArrival.clearTime())
    if(route == null)
        route = new Route()
//      route.stations.add(station)
//      route.stations.sort({it.position})
    def key = station?.estimatedArrival.clearTime()
    driver.routes[(key)] = (route)
}

有什么建议吗? (顺便说一句,我尝试将其更改为key.toString(),这使得异常消失) 提前谢谢!

编辑: 保存时出错:

@Transactional
def save(Job jobInstance) {
    if (jobInstance == null) {
        notFound()
        logNotFound("save", Job.class)

        return
    }

    setSaveConditions(jobInstance)
    setUpdateConditions(jobInstance)
    jobService.createStationsForJob(jobInstance)
    if(jobInstance.assignedUser)
        jobService.addStationsToRoute(jobInstance)
    jobInstance.validate()
    if (jobInstance.hasErrors()) {
        respond jobInstance.errors, view:'create'
        return
    }
    println 'routes: ' + jobInstance.assignedUser.routes.toString()
    jobInstance.save flush:true
    println 'routes: ' + jobInstance.assignedUser.routes.toString()
    logSaving(jobInstance)
    request.withFormat {
        form {
            flash.message = message(code: 'default.created.message', args: [
                message(code: 'job.label', default: 'Job'),
                jobInstance.encodeAsHTML()
            ])
            redirect jobInstance
        }
        '*' { respond jobInstance, [status: CREATED] }
    }
}

grails告诉我错误行是包含jobInstance.save flush的那个:true。 jobInstance有一个驱动程序属性,其中包含Map。当我使用String表示时,路由对象发生相同的错误:

Message: elektrova.Route cannot be cast to java.lang.String
Line | Method
->>   97 | $tt__save in elektrova.JobController$$EOZiJFlq
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    200 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread

当我使用路径的String表示时,保存就完成了。但是为什么世界上地图会有两个字符串呢?

EDIT2:

域类:

class Job implements Serializable{


JobStatus status = JobStatus.ACTIVE
Station startStation
Station endStation


String containerNumber
String loadingIndicator = false
String containerISO
String oversizeIndicator = false
Double grossWeight = 0
Boolean nonOperatingReeferIndicator = false
String iReference
String consigneeReference
String quayAccountNumber
String carrierCode
String bookingReference
String releaseNumber
String transportMeansIdentification
Boolean trainTransportIndicator = false
Boolean dangerousGoodsIndicator = false
String gegisReference
Boolean customsIndicator = false
String registrationNumber
String registrationPositon


Date creationTimeStamp
User updateUser
Date updateTimeStamp

boolean deleted = false

static belongsTo = [
    hauler:Hauler,
    assignedUser:Driver,
    creationUser:Disponent
]




static constraints = {
    hauler(blank:false, nullable:false)
    status(blank:false, nullable:false)
    endStation(nullable:true)
    startStation(nullable:true)     

    assignedUser(nullable:true)
    containerNumber(nullable:true)
    containerISO(nullable:true)
    iReference(nullable:true)
    consigneeReference(nullable:true)
    quayAccountNumber(nullable:true)
    carrierCode(nullable:true)
    bookingReference(nullable:true)
    releaseNumber(nullable:true)
    transportMeansIdentification(nullable:true)
    gegisReference(nullable:true)
    registrationNumber(nullable:true)
    registrationPositon(nullable:true)


    updateUser(nullable:true)
    updateTimeStamp(nullable:true)

    creationTimeStamp(blank:false, nullable:false)
    creationUser(blank:false, nullable:false)
}



def cancel(){
    this.status = JobStatus.CANCELLED
}

def finish(){
    this.status = JobStatus.DONE
}

//  @Override
//  def encodeAsHTML(){
 ////       destination.locationName + " (" + containerNumber + ") " + plannedArrivalTime.toString()
//  }
}

class Route {

//  List<Station> stations = []
//  
//  static belongsTo = [
//      hauler:Hauler,
//      driver:Driver
//  ]

//  static hasMany = [
//      jobs:Job
//  ]
}

1 个答案:

答案 0 :(得分:0)

您可以在地图声明中使用日期作为地图关键字,但您的问题实际上是关于修复数据库中存储问题的问题。