Search code examples
activejdbc

Is there a way to detach model object instances from the database?


I'm trying to figure out a way to serialize an activejdbc model from a server application running on a JVM across the wire to another JVM that is running a GUI application. The GUI application does not have access to the database, since it is not on the same machine. On the GUI, when I try to set the properties on that model instance via a setter, to update some fields to send the model back to the server to be updated, I obviously get exceptions about not having a database connection.

I have a way to get around this by overriding the getMetaModeLocal() method, but was wondering if there was a cleaner solution to this?

The exception is as follows:

Caused by: org.javalite.activejdbc.DBException: Failed to retrieve metadata from DB, connection: 'default' is not available
at org.javalite.activejdbc.Registry.init(Registry.java:133)
at org.javalite.activejdbc.Model.getMetaModel(Model.java:67)

I expect to be able to update the model without having to have a database connection until saveIt() has been called.


Solution

  • You have two options for this:

    1. Do not use models in your GUI app. On the server side you can serialize models into Maps by using a Model.toMap() method and simply sending a map to the UI. If you need to make updates, you can set new values to that same map and then send it back to the server side, where you can use a model.fromMap(map).save() approach. In other words, no need to send models across the wire.

    2. Wait till this is fixed: Add ability to use model classes without connecting to the DB. This new feature is currently in active development, and completion is projected within a couple of weeks. It will allow you to pass models from one JVM to another without the database because the DB schema will be queried during the build rather than at run time.