【问题标题】:grails JdbcSQLException in saving one-one mapping domain classgrails JdbcSQLException 在保存一对一映射域类
【发布时间】:2014-03-28 11:29:36
【问题描述】:

我是 grails 的新手,我创建了一个用户域类和 userprofile 域类。这些类是有关系的。 域类如下所示

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 但是当,使用相同的功能来更新我的用户配置文件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: anagkt@asdk.com
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

如何消除此异常.. 或错误
提前谢谢

【问题讨论】:

    标签: grails jdbc grails-2.0 grails-domain-class


    【解决方案1】:

    尝试保存域并引用未保存的对象并尝试引用时发生约束错误。所以做一些欺骗性的例子:你也应该做唯一的用户

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

    上面的示例将保存面部和鼻子。请注意,逆向不正确,并且会由于瞬态 Face 导致错误:

    MORE

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

    【讨论】:

    • 这里它第一次运行良好..我实际上不知道......但在这种情况下,我真的想保存独立于用户配置文件的用户。并且在保存用户配置文件时,我想使用 hasone 关系使用用户配置文件更新用户。所以我想如何更改我的代码..我对这个映射感到困惑..@danielad
    • 我尝试了独特的约束,但它第一次不起作用并显示与 jdbc.@danielad 相关的错误
    • |错误 2014-03-28 17:32:36,719 [http-bio-8530-exec-6] 错误 util.JDBCExceptionReporter - 未设置参数“#1”; SQL语句:选择this_.id为id3_0_,this_.version为version3_0_,this_.account_expired为account3_3_0_,this_.account_locked为account4_3_0_,this_.email为email3_0_,this_.enabled为enabled3_0_,this_."password"为password7_3_0_,this_。 password_expired 为 password8_3_0_,this_.status 为 status3_0_,this_.username 为 username3_0_,来自用户 this_ where this_.id=? [90012-164]
    • 我在对用户类中的个人资料使用 unique :true 时出现错误。并且第一次无法正常工作。当我删除这个独特的约束时,它第一次可以正常工作@danielad
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多