Question

I'm using ObjectDB but also want to make the collection inside a persisted object observable, so I have declared it this way:

@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL) List<Widget> widgets = [] as ObservableList

Later on, I create a listener closure and attach it:

    def widgetChangeListener = {
        log.debug "WIDGET CHANGE: $it"
    }

    widgets.addPropertyChangeListener(widgetChangeListener)

However, when I try to persist the collection, I get this error:

Attempt to store an instance of a non persistable type com.greymatter.strategy.Harness$_closure1 - field com.greymatter.strategy.Harness.widgetChangeListener (error 303)

Is there any way to make this collection persistable while keeping the closure volatile, so I can observe changes to it? ObjectDB has a @Transient annotation, but I'm not sure how to apply it to the closure. If I put it on the def of widgetChangeListener, I get a MissingMethodException.

Are ObjectDB and ObservableList mutually exclusive?

Was it helpful?

Solution

Groovy collections are not fully supported by ObjectDB (and by JPA in general). See the list of supported collections in JPA / ObjectDB on this ObjectDB Manual page.

If ObservableList works well, except the listeners, you may use JPA lifecycle events to clear listeners before persisting or updating the entity object (and then set them back if necessary).

Alternatively you can keep 2 list fields in your entity class. An ordinary List that will be persisted, and an ObservableList wrapper of that list that will be set as transient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top