Search code examples
grailsgroovygrails-ormcomposition

GORM Composition save error, Could not find matching constructor for: java.lang.String(java.lang.Long)


I am trying to save a reservation that has a student id, where Student extends User.

When I save I get the "could not find matching constructor error for java.lang.String(java.lang.Long)."

From what I know, the Long that it's trying to send is the unique id from the Student object. I can't find a workaround, here's my code.

 class User {
   static hasMany = [reservations:Reservation, students:Student, faculty:Faculty]

   String username
   String password
   String firstName
   String lastName
   String phoneNumber
   String emailAddress
   boolean enabled
   boolean accountExpired
   boolean accountLocked
   boolean passwordExpired



 class Student extends User{
     static belongsTo = [users:User]
     static hasMany = [reservations:Reservation]

     String studentId

 class Reservation {    
     def securityService
     Student student
     String reservationId
     String lastUpdated
     String lastUpdatedBy
     String startDate
     String endDate

When I use reservation1.save() I get an error:

Message: Could not find matching constructor for: java.lang.String(java.lang.Long)

Here is the data that I want to save

//UserData
def user1 = Student.findByUsername('bobby') ?: new Student(username: 'bobby',
    enabled: true, password: 'pass', firstName: 'robert', lastName:'plant',
    metroStateStudentId: '012345670', phoneNumber: '0123456789',

//Reservation Data
def reservation1 = Reservation.findByReservationId('123456') ?: new   Reservation(reservationId:'123456',
    student: user1,startDate:'2013-07-23', endDate:'2013-07-27',lastUpdatedBy:'bobby')
    reservation1.save(flush: true, failOnError: true)

Solution

  • Not sure how complete your example code above is, but I see a couple of things which may or may not be causing the exception you're seeing.

    1. def securityService (in Reservation) should be transient so gorm doesn't try to persist. BTW - did you mean springSecurityService
    2. UPDATE: just realize you've declared them as Strings I don't think you can set dates directly like that, you need to parse the String for example Date().parse("yyyy-MM-dd", '2013-07-23'))
    3. if you're using GORM autoTimestamp, the lastUpdated field should be a Date not String - otherwise you should disable it if you're not using it
    4. finally, is Reservation mapped to a legacy database - is there a reason to have String reservationId vs. default id provide by GORM?