2014-03-28 48 views
3

我是新來的grails,我創建了一個用戶域類和userprofile域類。而這些課程都是具有實用性的。 域類如下grails JdbcSQLException保存一對一的映射域類

class User { 

    transient springSecurityService 

    String username 
    String password 
    String email 
    static hasOne = [profile: UserProfile] 
    boolean enabled = true 
    boolean accountExpired 
    boolean accountLocked 
    boolean passwordExpired 

    static transients = ['springSecurityService'] 

    static constraints = { 
     username blank: false, unique: true 
     password blank: false 
     email blank: false, nullable: false, unique: true, email: true 
     profile nullable:true, unique: true 
    } 
class UserProfile { 

    String firstname; 
    String team; 
    String pidgin; 
    String phone; 
    User user 

    static constraints = { 
     firstname nullable:true 
     team nullable:false, blank:false 
     pidgin nullable:false, blank:false 
     phone nullable:false 
    } 
} 

在我的服務類

class UserService{ 

public User createUserProfile(UserProfile profile,String email) { 
     User user = User.findByEmail(email) 
     if(!user) 
      //no user found 
     profile.user = user 
     profile.save() 
     user.profile = profile 
     user.save(failOnError: true) 
    } 
} 

運行我的項目時給予它的工作原理確切方式的任何和執行createUserProfile 但是當使用相同的功能來更新我的USERPROFILE user.save(failOnError: true) 引發JdbcSQLException。

詳細的錯誤在下面給出。

| Error 2014-03-28 16:21:28,958 [http-bio-8530-exec-8] ERROR util.JDBCExceptionReporter - Unique index or primary key violation: "CONSTRAINT_INDEX_C ON PUBLIC.USER_PROFILE(USER_ID)"; SQL statement: 
insert into user_profile (id, version, firstname, phone, pidgin, team, user_id) values (null, ?, ?, ?, ?, ?, ?) [23505-164] 
| Error 2014-03-28 16:21:29,065 [http-bio-8530-exec-8] ERROR errors.GrailsExceptionResolver - JdbcSQLException occurred when processing request: [POST] /OrbiFlow/user/profileEditSubmit - parameters: 
phone: 4568932158 
username: ani 
email: [email protected] 
pidgin: weg 
team: sdgv 
firstname: qwf 
Unique index or primary key violation: "CONSTRAINT_INDEX_C ON PUBLIC.USER_PROFILE(USER_ID)"; SQL statement: 
insert into user_profile (id, version, firstname, phone, pidgin, team, user_id) values (null, ?, ?, ?, ?, ?, ?) [23505-164]. Stacktrace follows: 
Message: Unique index or primary key violation: "CONSTRAINT_INDEX_C ON PUBLIC.USER_PROFILE(USER_ID)"; SQL statement: 
insert into user_profile (id, version, firstname, phone, pidgin, team, user_id) values (null, ?, ?, ?, ?, ?, ?) [23505-164] 
    Line | Method 
->> 329 | getJdbcSQLException  in org.h2.message.DbException 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 169 | get      in  '' 
| 146 | get . . . . . . . . . . in  '' 
| 81 | getDuplicateKeyException in org.h2.index.BaseIndex 
| 62 | add . . . . . . . . . . in org.h2.index.TreeIndex 
| 50 | add      in org.h2.index.MultiVersionIndex 
| 121 | addRow . . . . . . . . . in org.h2.table.RegularTable 
| 124 | insertRows    in org.h2.command.dml.Insert 
| 84 | update . . . . . . . . . in  '' 
| 73 | update     in org.h2.command.CommandContainer 
| 226 | executeUpdate . . . . . in org.h2.command.Command 
| 143 | executeUpdateInternal in org.h2.jdbc.JdbcPreparedStatement 
| 129 | executeUpdate . . . . . in  '' 
| 105 | executeUpdate   in org.apache.commons.dbcp.DelegatingPreparedStatement 
| 83 | createUserProfile . . . in com.orb.user.UserService 
| 178 | profileEditSubmit  in com.orb.user.UserController 
| 195 | doFilter . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter 
| 63 | doFilter     in grails.plugin.cache.web.filter.AbstractFilter 
| 53 | doFilter . . . . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter 
| 49 | doFilter     in grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter 
| 82 | doFilter . . . . . . . . in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter 
| 886 | runTask     in java.util.concurrent.ThreadPoolExecutor$Worker 
| 908 | run . . . . . . . . . . in  '' 
^ 662 | run      in java.lang.Thread 

如何刪除此異常..或錯誤
thankz提前

回答

3

當試圖保存域,且引用該對象,它是未保存,並試圖指出現約束錯誤。所以做一些欺騙的例子,如:你也應該做出獨特的用戶

new Face(nose:new Nose()).save() 

上面的例子將保存面部和鼻子。需要注意的是逆是不正確的,並會導致錯誤由於瞬態面:

MORE

焦點:
Message: Unique index or primary key violation: "CONSTRAINT_INDEX_C ON PUBLIC.USER_PROFILE(USER_ID)"; SQL statement:

+2

在這裏工作得很好第一次..我不知道實際。 ..但在這裏,我這種情況下,我真的想要保存獨立於userprofile的用戶。並保存用戶配置文件時,我想使用hasone關係用userprofile更新用戶。所以我想如何改變我的代碼..我與這種映射混淆.. @danielad –

+1

@AnIsh你試過獨特的約束? – danielad

+0

我嘗試了獨特的構思,但是它不能在第一次使用,並顯示與jdbc相關的錯誤。@ danielad –