grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.79k stars 949 forks source link

addTo* dynamic method is not supported in GORM standalone #598

Closed restagner closed 9 years ago

restagner commented 9 years ago

I've been wrangling my brain over the past couple of days with regards to some code I'm attempting to use in GORM Standalone mode. If I run the following code using the grails console command (i.e., using grails 2.4.4)

import com.xyz.domain.entities.*

def license = new License()
license.with {
    project = 'Automated License Test'
    projectTypeId = 2 // Trailer
    fee = new BigDecimal("35000.00")
    feeCurrencyId = 153 // US Dollars
    licenseeId = 661 // Paramount
    contractDate = new Date() - 10
    licenseStatusId     = '1' // In Progress
    licenseTermId = 'P' // perpetual
    setupById = 'testr005'
    setupDate = new Date()
}

def master = new Master()
master.with {
    title = 'Automated Master Test'
    originalLabelId = 'WDR'
    duration = '3:44'
    recordingTypeId = 17
    languageId = 21
    pNoticeYear = '2014'
    pNoticeText = 'Walt Disney Records'
    recordingDateText = '2014'
    aftraInd = 'Y'
    afmInd = 'Y'
    countryOfOriginId = 100
    assetTypeId = 1
    setupById = 'testr005'
    setupDate = new Date()
    versionTitle = 'From the TEST Soundtrack'
    englishTitle = 'Automated Master English Title'
}

license.addToMaster(master)
license.save(failOnError: true)
println "LICENSE ID: ${license.id}"
println " MASTER ID: ${master.id}"

Everything works as expected. I get data/records added to the master, license and license_master (the join table) tables. Yet, when I attempt to run the same code using GORM in standalone mode ... like so

@Grab("org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE")
@Grab("Microsoft:mssql-driver-jdbc4:4.0")
@GrabExclude('org.codehaus.groovy:groovy-all')

import grails.persistence.*
import grails.orm.bootstrap.*
import com.microsoft.sqlserver.jdbc.SQLServerDriver
import org.springframework.jdbc.datasource.DriverManagerDataSource

def dbHostName   = 'db.host.name.com'
def dbPortNumber = '8888'
def dbName       = 'dnName'
def dbUsername   = 'username'
def dbPassword   = 'password'
def jdbcUrl      = "jdbc:sqlserver://${dbHostName}:${dbPortNumber};databaseName=${dbName}"

def entities = []
entities.add(Master)
entities.add(License)

// initialize the _list_ of Entities
def init = new HibernateDatastoreSpringInitializer(entities)
// set up the data source connection
def dataSource = new DriverManagerDataSource("${jdbcUrl}", "${dbUsername}", "${dbPassword}")
dataSource.setDriverClassName(SQLServerDriver.name)
init.configureForDataSource(dataSource)

def license = new License()
license.with {
    project = 'Automated License Test'
    projectTypeId = 2 // Trailer
    fee = new BigDecimal("35000.00")
    feeCurrencyId = 153 // US Dollars
    licenseeId = 661 // Paramount
    contractDate = new Date() - 10
    licenseStatusId     = '1' // In Progress
    licenseTermId = 'P' // perpetual
    setupById = 'testr005'
    setupDate = new Date()
}

def master = new Master()
master.with {
    title = 'Automated Master Test'
    originalLabelId = 'WDR'
    duration = '3:44'
    recordingTypeId = 17
    languageId = 21
    pNoticeYear = '2014'
    pNoticeText = 'Walt Disney Records'
    recordingDateText = '2014'
    aftraInd = 'Y'
    afmInd = 'Y'
    countryOfOriginId = 100
    assetTypeId = 1
    setupById = 'testr005'
    setupDate = new Date()
    versionTitle = 'From the TEST Soundtrack'
    englishTitle = 'Automated Master English Title'
}

license.addToMaster(master)
license.save(failOnError: true)
println "LICENSE ID: ${license.id}"
println " MASTER ID: ${master.id}"

@Entity
class Master {

    String title
    String originalLabelId
    String duration
    Integer recordingTypeId
    String assetType
    String isrc
    String sapIssueNum
    Short languageId
    String pNoticeYear
    String pNoticeText
    String recordingDateText
    Integer territoryId
    String locationRecording
    String locationMixing
    String locationMastering
    Character aftraInd
    Character afmInd
    String courtesyOfText
    String comments
    Character parentalAdvisoryInd
    Character status
    Long royaltyMasterId
    Integer countryOfOriginId
    Character containSamples
    String miscLabelCopy
    Integer assetTypeId
    String setupById
    Date setupDate
    String updatedById
    Date updatedDate
    String englishTitle
    String versionTitle
    Integer parentalIndId
    String isrcCloned
    Character sendToUmgInd
    Date sendToUmgDate

    static hasMany = [license: License]
        static belongsTo = [License]

    static mapping = {
        id column: "master_id"
        version false
    }

    static constraints = {
        originalLabelId maxSize: 3
        duration nullable: true
        assetType nullable: true
        isrc nullable: true
        sapIssueNum nullable: true, maxSize: 12
        languageId nullable: true
        pNoticeYear nullable: true
        pNoticeText nullable: true
        recordingDateText nullable: true
        territoryId nullable: true
        locationRecording nullable: true
        locationMixing nullable: true
        locationMastering nullable: true
        aftraInd nullable: true, maxSize: 1
        afmInd nullable: true, maxSize: 1
        courtesyOfText nullable: true
        comments nullable: true
        parentalAdvisoryInd nullable: true, maxSize: 1
        status nullable: true, maxSize: 1
        royaltyMasterId nullable: true
        countryOfOriginId nullable: true
        containSamples nullable: true, maxSize: 1
        miscLabelCopy nullable: true
        assetTypeId nullable: true
        setupById nullable: true
        updatedById nullable: true
        updatedDate nullable: true
        englishTitle nullable: true
        versionTitle nullable: true
        parentalIndId nullable: true
        isrcCloned nullable: true
        sendToUmgInd nullable: true, maxSize: 1
        sendToUmgDate nullable: true
    }
}

@Entity
class License {

        Integer artistId
        Integer territoryId
    String project
    Integer projectTypeId
    String labelId
    BigDecimal fee
    Integer feeCurrencyId
    Integer licenseeId
    Date contractDate
    Character licenseStatusId
    Character licenseTermId
    Date termEndDate
    String notes
    String setupById
    Date setupDate
    String updatedById
    Date updatedDate
    String termNotes
    Integer feeId
    String royaltyTerm
    String licenseStatusNotes
    Integer mediaTypeId
    String mediaTypeNotes
    Character optionInd
    String optionNotes
    String territoryNotes
    Integer licenseProductId
    Date releaseDate
    Date returnDate
    String licenseeContact
    BigDecimal amountReceived
    BigDecimal balanceOfAdvance
    BigDecimal royaltiesEarned
    Integer units

    static hasMany = [master: Master]

    static mapping = {
        id column: "license_id"
        version false
    }

    static constraints = {
                artistId nullable: true
                territoryId nullable: true
        projectTypeId nullable: true
        labelId nullable: true, maxSize: 3
        fee nullable: true, scale: 4
        feeCurrencyId nullable: true
        licenseeId nullable: true
        contractDate nullable: true
        licenseStatusId nullable: true, maxSize: 1
        licenseTermId nullable: true, maxSize: 1
        termEndDate nullable: true
        notes nullable: true
        setupById nullable: true, maxSize: 10
        setupDate nullable: true
        updatedById nullable: true, maxSize: 10
        updatedDate nullable: true
        termNotes nullable: true, maxSize: 4000
        feeId nullable: true
        royaltyTerm nullable: true, maxSize: 50
        licenseStatusNotes nullable: true, maxSize: 4000
        mediaTypeId nullable: true
        mediaTypeNotes nullable: true
        optionInd nullable: true, maxSize: 1
        optionNotes nullable: true, maxSize: 4000
        territoryNotes nullable: true
        licenseProductId nullable: true
        releaseDate nullable: true
        returnDate nullable: true
        licenseeContact nullable: true
        amountReceived nullable: true, scale: 4
        balanceOfAdvance nullable: true, scale: 4
        royaltiesEarned nullable: true, scale: 4
        units nullable: true
    }
}

It fails with the following exception

INFO: HHH000232: Schema update complete
Caught: groovy.lang.MissingMethodException: No signature of method: License.addToMaster() is applicable for argument types: (Master) values: [Master : (unsaved)]
Possible solutions: getMaster()
graemerocher commented 9 years ago

This is already fixed in master

restagner commented 9 years ago

@graemerocher

This is great news!!

I'm just getting around to reading this information. As noted in this ticket, we are using GORM-Standalone (version 1.1.0.RELEASE) using the following syntax

@Grab("org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE")
@Grab("Microsoft:mssql-driver-jdbc4:4.0")
@GrabExclude('org.codehaus.groovy:groovy-all')

// initialize the _list_ of Entities
def init = new HibernateDatastoreSpringInitializer(entities)
// set up the data source connection
def dataSource = new DriverManagerDataSource("${jdbcUrl}", "${dbUsername}", "${dbPassword}")
dataSource.setDriverClassName(SQLServerDriver.name)
init.configureForDataSource(dataSource)

If/when I move to version 3.x.x, can I re-use the same code? If not, could you please provide me with an example of how I may use GORM-Standalone with version 3.x.x?

graemerocher commented 9 years ago

Yes you can reuse the same code

restagner commented 9 years ago

@graemerocher very nice!!! How about the dependency? At the moment, I'm using

@Grab("org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE")

Currently, I do not see the equivalent for version 3.0.3 in Maven Central

graemerocher commented 9 years ago

@restagner They haven't been published to central but are on Bintray:

https://bintray.com/grails/grails-core/org.grails%3Agorm-hibernate4-spring-boot/view

restagner commented 9 years ago

Thanks @graemerocher ! I'll give that a try

restagner commented 9 years ago

@graemerocher

My apologies for returning to this thread. But, I gave the update a try and it did not work out for me. I've posted questions to the Grails Slack channel as well as posted my issue on stackoverflow (http://stackoverflow.com/questions/32315048/gorm-standalone-throws-hibernateexception). However, I have yet to get a response. That is why I'm back here hoping that you can help me out.

Basically, after upgrading to gorm-hibernate4-spring-boot:4.0.4.RELEASE, I receive the following exception

HibernateException: No Session found for current thread

The stackoverflow link I referenced above has all the details. I hope that you can help out. I'm about ready to give up on the standalone upgrade.