2014-02-27 18 views
2

我有4個對象(域類的實例),稱爲出租車,放置在屬性assignedTaxi下的一個名爲moveRequest的對象。如何從數據庫grails重新加載一個域類的實例?

的類是這樣的:

class MoveRequest{ 

Taxi allocatedTaxi 
Zone destinationZone 
Integer destination 
String microDestination 
String targetType = "" 
String targetName = "" 
Zone currentTaxiZone 
Taxi obstructingTaxi 
Taxi boardedTaxi 

String state = "pending" 
Boolean requestedSpecific 
Date dateCreated 

static constraints = { 
    currentTaxiZone(nullable: true) 
    obstructingTaxi(nullable: true) 
    boardedTaxi(nullable: true) 
} 

static mapping = { 
    version false 
} 

}

class Taxi { 

String name 
Integer position 
Double voltage 
Boolean busy 
Integer orderNumber 

static belongsTo = [rail: Rail] 

static mapping = { 
    version false 
} 

static constraints = { 
    name blank: false 
} 

}現在

,而迭代moveRequests使用SQL語句我更新的出租車對象的位置屬性功能如下:

def updateTaxiPosition(def taxiName, def millimeters){ 
    def config = grailsApplication.config 
    def sql = Sql.newInstance(config.grails.databaseURL, config.grails.databaseUsername, config.grails.databasePassword, config.grails.databaseDriverClassName) 

    sql.getConnection().setAutoCommit(true) 
    sql.execute("UPDATE taxi SET position= ? WHERE name = ?", [millimeters, taxiName]) 
    sql.close() 
} 
現在

,其他的方式做到這一點是使用這行代碼:

moveRequest.allocatedTaxi.position = millimeters 

但是,因爲我需要使用直接SQL更新它快速的IM,因爲它會自動更新出租車位置。現在,當我嘗試訪問已更改的位置時,問題就出現了。我只能訪問舊的位置,即使位置在數據庫中已經改變,當我嘗試這樣做:

println("taxi position: " + moveRequest.allocatedTaxi.position) 

我更新的位置屬性之前使用舊的價值。現在,我試圖刷新moveRequest情況是這樣的:

def refreshMoveRequestInformation(def moveRequest){ 
    def config = grailsApplication.config 
    def sql = Sql.newInstance(config.grails.databaseURL, config.grails.databaseUsername, config.grails.databasePassword, config.grails.databaseDriverClassName) 

    sql.getConnection().setAutoCommit(true) 
    def row = sql.rows("SELECT * FROM taxi WHERE id=?", [moveRequest.allocatedTaxi.id]) 
    println("row -> taxi name: " + row.name + ", taxi position: " + row.position) 
    sql.close() 

    def newPosition = null 
    row.each{ 
     newPosition = it.position 
    } 
    moveRequest.allocatedTaxi.position = newPosition 
} 

而且這樣的:

def refreshMoveRequestInformation(def moveRequest){ 
    def taxi = Taxi.get(moveRequest.allocatedTaxi.id) 
    println("1 - new taxi name: " + taxi.name + ", taxi position: " + taxi.position) 
    taxi.refresh() 
    println("2 - new taxi name: " + taxi.name + ", taxi position: " + taxi.position) 

    println("move request -> taxi name: " + moveRequest.allocatedTaxi.name + ", taxi position: " + moveRequest.allocatedTaxi.position) 

    return moveRequest 
} 

但沒有它似乎工作。即使行實例顯示新值,我仍然會收到舊值。我如何更新moveRequest實例(通過我的意思是從數據庫重新加載)它的分配的屬性(或更準確地說,位置屬性)?

回答

2

你可以做兩件事。

首先,在您的域實例上調用.refresh()將確保它從SOR(記錄系統)(在此情況下爲您的數據庫)加載。

其次,您可能希望從Hibernates第二級cache中刪除此域類。

class SomeClass { 
    static mapping = { 
    cache false 
    } 
} 
+0

但問題是,我不認爲刷新()的作品,因爲當它被稱爲上了出租車例如,它已被執行更新之前剛剛返回相同的值。我不明白爲什麼,這就像它在進入事務時記得它的價值,然後無論數據庫中發生了什麼,都可以簡單地使用相同的值。 – miso

+0

嘗試從Hibernates緩存中刪除它。如果您處於交易中,更新是孤立的,則完全有可能。交易的範圍是什麼? –

+0

所以我應該嘗試向出租車類添加緩存錯誤?我不知道這將如何幫助,你會介意解釋嗎?我相信事務的範圍將用於管理moveRequests的整個函數,因爲所有函數都放在一個服務中,所以使用另一行(moveRequest.allocatedTaxi.position = millimeters)所做的更改可以在數據庫中看到只有在使用該行更新了所有4個實例後,纔對4個moveRequests的迭代結束。對不起,如果我複雜,超過我應該xD – miso

相關問題