grails JdbcSQLException 在保存一对一映射领域类

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

我是 grails 的新手,我创建了一个用户域类和用户配置文件域类。而这些类(class)是有亲戚关系的。 域类如下所示

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

如何消除这个异常..或错误
提前致谢

最佳答案

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

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

上面的例子将保存面部和 Nose 。请注意,反之则不然,会因 transient 面而导致错误:

MORE

焦点:
消息:唯一索引或主键违规:“CONSTRAINT_INDEX_C ON PUBLIC.USER_PROFILE(USER_ID)”; SQL语句:

关于grails JdbcSQLException 在保存一对一映射领域类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22711625/

相关文章:

grails 数据绑定(bind)集合

hibernate - 使用不可变嵌入对象的 Grails GORM

grails - 将 Grails 项目用于公共(public)代码和对其他 Grails 项目的依赖

unit-testing - 如何在集成测试中为动态脚手架Grails Controller 模拟服务?

java - 连接关闭时结果集未关闭?

java - Matcher.quoteReplacement 与 preparedstatement

grails - Grails 2,战法-> java.lang.ClassNotFoundException:grails.test.mixin.services.ServiceUnitTestMixin

grails - 如何在Grails 3中使用Gradle Cobertura插件

forms - Grails 具有动态表单的一对多数据绑定(bind)

java错误(找不到合适的驱动程序)