2014-01-16 24 views
1

我需要知道某個域類的某個屬性是否是主鍵。有沒有辦法做到這一點?無論如何檢查域類中的屬性是否是主鍵?

例如,我有一個域類Person.groovy(使用hibernate),我動態加載這個類屬於一個單獨的插件。

class Person{  
static mapping = { 
    table 'PERSON' 
    // version is set to false, because this isn't available by default for legacy databases 
    version false 
    id column:'PERSON_ID' 
} 
Long personId 
String name 
String address 
static constraints = {} 
} 

我有一個動態加載域類另一個類,它需要檢查屬性是否是主鍵或沒有。

回答

1

你可以得到一個域對象的標識屬性:

// Get a single Person and interrogate the domainClass 
def idProp = Person.find {}.domainClass.identifier 

那麼你應該能夠調用methods on the GrailsDomainClassProperty interface之一,即:

println idProp.name 
+0

嗨。非常感謝您的回覆。但是,當我嘗試獲取標識符時,即使我的標識符具有不同的名稱,我也總是會得到結果「id」。 – user3200957

相關問題