Grails 2.4.2: Strange cascading save behaviour in

2019-07-30 12:36发布

I have some strange behaviour with incorrect cascading of the save in my object graph. Maybe it has something to do with nested transaction, but maybe not.

Here are my 3 domain classes:

Station.groovy

class Station {

  /** Dependency injected {@link de.hutapp.tapandknow.system.SettingsService}.*/
  def settingsService

  static hasMany = [localizedStations: LocalizedStation, 
    stationIdentifiers: AbstractStationIdentifier]

}

LocalizedStation.groovy

class LocalizedStation {

  /** Delete-cascade from the {@link Station} parent. */
  static belongsTo = [station : Station]

  /** An image that represents the LocalizedStation. */
  MediaFile headerImage

  /** The articles inside this LocalizedStation .*/
  List<Article> articles

  static hasMany = [articles: Article]

  static mapping = {
      headerImage cascade: 'all'
      articles cascade: 'all-delete-orphan'
  }
}

MediaFile.groovy

class MediaFile {

  /* some basic properties here*/

  static constraints = {
      name blank: false
      originalFilename blank: false
      description blank: false
  }

  def beforeDelete() {
      // delete the associated file
      new File(getFileLocation()).delete()
  }
}

So the object graph is Station->LocalizedStation->MediaFile

I save/update the objects with the following service methods:

StationService.groovy

@Transactional
class StationService {

def mediaFileService

/**
 * Creates a new {@link Station} and saves it.
 * @return The newly created {@link Station} instance.
 */
Station createStation(LocalizedStation localizedStationInstance, InputStream headerImage, String originalFilename) {

    localizedStationInstance = updateLocalizedStation(localizedStationInstance, headerImage, originalFilename)

    Station stationInstance = new Station()
    stationInstance.addToLocalizedStations(localizedStationInstance)
    stationInstance.save()
    stationInstance.localizedStations[0].headerImage.save() // TODO: check why this is necessary

    return stationInstance
}

LocalizedStation updateLocalizedStation(LocalizedStation localizedStationInstance, InputStream headerSource, String originalFilename) {

    if (headerSource) {
        MediaFile headerFile = mediaFileService.createMediaFile(headerSource, originalFilename, "foobar") // TODO: add description
        localizedStationInstance.headerImage = headerFile
    }

    localizedStationInstance.save()
    return localizedStationInstance

}
}

MediaFileService.groovy

@Transactional
class MediaFileService {

MediaFile createMediaFile(InputStream input, String originalFilename, String description) {

    MediaFile mediaFileInstance = new MediaFile(
            name: originalFilename,
            description: description)

    mediaFileInstance.save(flush: true) // flush for id
    mediaFileInstance.storeMediaFile(input, originalFilename)
    mediaFileInstance.save()

    return mediaFileInstance
}
}

As you can see, if I call stationService.createStation(), I have to manually save the MediaFile, that hangs on the LocalizedStation. This only happens if I call createStation(), which itself calls updateLocalizedStation(). Then the MediaFile won't be persisted unless I save it explicitly in createStation().

If I call updateLocalizedStation() directly, everything works as expected.

Any ideas or suggestions?


UPDATE:

This problems dissappears, if I add @Transactional(propagation=Propagation.REQUIRES_NEW) to the createMediaFile() method.

As far as I understand the documentation, this creates a new transaction. But this is not really what I want, I want a transaction, that spans all methods.

0条回答
登录 后发表回答