gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:05 25 4
gpt4 key购买 nike

这是我的问题:我正在尝试填充以下 map :

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

经过一番研究,我发现将 key 放在括号中应该可以解决问题(来源: http://groovy.codehaus.org/Collections map 部分 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.savelush:true 的行。 jobInstance有一个driver属性,其中包含Map。当我使用字符串表示形式时,路由对象会发生相同的错误:

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

当我使用路线的字符串表示时,保存完成。但为什么 Map 需要两个字符串呢?

编辑2:

域类:

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
// ]
}

最佳答案

您可以像在 map 声明中那样使用日期作为 map 键,但您的问题实际上是关于解决其在数据库中存储的问题。

关于java - 如何在 Groovy 中使用 java.util.Date 作为映射键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22682358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com