3

I am new to grails , i have created a user domain class and userprofile domain class. and these class are hasone realtionship. the domain class is given below

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
    }
}

in my service class

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)
    }
}

when running my project it works exact way any and executes createUserProfile but when, using the same function to update my userprofile user.save(failOnError: true) throws an JdbcSQLException.

the detailed error is given below.

| 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

how can remove this exception.. or error
thankz in advance

Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
An Ish A
  • 711
  • 5
  • 18

1 Answers1

3

A constraint error occurs when trying to saving the domain and referencing that object which is unsaved and trying to refer. So do some tricking examples like : you should also make unique User

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

The example above will save both face and nose. Note that the inverse is not true and will result in an error due to a transient Face:

MORE

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Develop4Life
  • 7,581
  • 8
  • 58
  • 76
  • 2
    here it works fine for first time.. i dont know actually...but here i this case I really want to save user independent of userprofile. and when saving user profile i want to update user with userprofile using hasone relation. so how i want to change my code .. i ama confused with this mapping.. @danielad – An Ish A Mar 28 '14 at 11:56
  • i tried unique constrine but at that it doesn't work for first time and shows error related to jdbc.@danielad – An Ish A Mar 28 '14 at 12:01
  • 2
    | Error 2014-03-28 17:32:36,719 [http-bio-8530-exec-6] ERROR util.JDBCExceptionReporter - Parameter "#1" is not set; SQL statement: select this_.id as id3_0_, this_.version as version3_0_, this_.account_expired as account3_3_0_, this_.account_locked as account4_3_0_, this_.email as email3_0_, this_.enabled as enabled3_0_, this_."password" as password7_3_0_, this_.password_expired as password8_3_0_, this_.status as status3_0_, this_.username as username3_0_ from user this_ where this_.id=? [90012-164] – An Ish A Mar 28 '14 at 12:06
  • I sthe error while using unique :true for profile in user class.. And not working for first time.. when i remove this unique constrian it works fine for first time@danielad – An Ish A Mar 28 '14 at 12:07