2013-07-08 20 views
0

我有一個域類UserProfile,它與另一個域類User具有一對一的關係。提供了域的結構。Grails;如何在本機SQL查詢中引用域屬性

сlass UserProfile { 
String fio 
String position 
String phone 
String email 

static belongsTo = [user: User] 

static constraints = { 
      // some constraints 
} 
static mapping = { 
    //some mapping; user property is not mapped 
}  

我需要寫Grails中爲用戶配置域名原生SQL查詢,我不知道如何引用用戶屬性(靜態屬於關聯= [用戶:用戶])。我嘗試過USER_ID,但它不起作用。 我無法直接使用映射部分命名該列;我只需要了解UserProfile域中的user列如何在數據庫中命名,以及如何在本機sql查詢中調用它。

+2

如果您只需要查看模式細節,然後運行'grails schema-export',則會生成目標爲/ ddl.sql的模式。 http://grails.org/doc/2.2.1/ref/Command%20Line/schema-export.html – ikumen

回答

1

很簡單,如果我得到你的問題是,Grails GORM約定在數據庫中存儲的Fileds是:

user_profile for UserProfile -Domain 

並且所有的文件都通過下劃線進行拼寫,並且大部分時間gorm在外鍵引用/或G之後添加_id像上面一對一和一個ORM關係到許多

[belongsTo=[user]] . 

中的SQL表

mysql describe user_profile ; 

    ---------------------------------------------------------------- 
    User_Profile 
    ---------------------------------------------------------------- 
    id 
    version 
    foo   varchar(50) 
    postion 
    email 
    user_instance_id int 
    ------------------------------------------------------------------- 

原生SQL查詢將是:

'select up.user_instance_id from user_profile as up ' 

通過查詢,獲取所有的userInstance對象用戶表

'select * from user where user.id = 'the id you get it from the above query' 

我希望你對此有一些想法,如果我沒有得到它,讓我知道。

-1

我相信如果你在UserProfile中定義用戶,那麼你可以訪問它並自動映射?它在我以前的項目中工作,我希望它能與此一起工作。

сlass UserProfile { 
String fio 
String position 
String phone 
String email 

static belongsTo = [user: User] 

User userInstance; 

static constraints = { 
      // some constraints 
} 

然後你可以使用它

UserProfile.executeQuery("select up.userInstance from UserProfile up") 
+0

這不是原生SQL。這是HQL。 – Gregg

+0

這個例子是HQL,但是在SQL中你仍然可以引用屬性yes? – JavaDev

相關問題