Community contributed extensions

Inside the model class

The PlayMorphia Model defines the skeleton for user entity class to interact with MongoDB. It defines a group of interfaces in a single class:

Some interfaces are defined in dynamic (instance) methods while others are in static methods:

Dynamic interfaces

Dynamic interfaces are methods binding to the model instance, e.g. each entity object.

  1. Java common object contract
    • toString, hashCode, equals
  2. Implement play.db.Model
    • _key, _save, _delete
  3. Simulate play.db.jpa.GenericModel
    • create, refresh, edit, save, merge, delete, validateAndSave, validateAndCreate(new in 1.2.4)
  4. CRUD
    • create, refresh, save, merge, delete
  5. Timestamp
    • _getCreated, _getModified
  6. ID relevant methods
    • getId, setId, processId_, isUserDefinedId_
  7. Lifecycle handling (NOT to be used by application or plugin developers)
    • h_*, deleteBlobs, deleteBlobsInBatch
  8. GridFS/Blob field handling (NOT to be used by application or plugin developers)
    • saveBlobs, deleteBlobs, deleteBlobsInBatch, getBlobFileName

Static interfaces

Static interfaces are methods binding to the model class, e.g. each entity type.

  1. Simulate play.db.jpa.GenericModel
    • create, edit, getModelFactory
  2. CRUD
    • get,delete, deleteAll
  3. Query interfaces
    • all, find, findAll, findById, q, filter
  4. GridFS/Blob field handling (NOT to be used by application or plugin developers)
    • removeGridFSFiles, getBlobFileName
  5. low level interface: ds, db

It’s not difficult to understand why we have provided those dynamic interfaces in the model but an OOP veteran will quickly ask the question that how those static interfaces could be reused. For example, suppose the application developer create an new model Order extends morphia model, how it comes calling on Order.all() returns all Order entities as static methods are not virtual.


The magic is behind Play’s code enhancement mechanism. When the Play application bootup, Play framework will call class enhancer defined in PlayMorphia module to weave certain logic into application classes. These include most of the static methods mentioned above. The calling to lifecycle annotation marked methods all also weaved by the class enhancer. This process is a powerful mechanism for Play plugins like PlayMorphia to provide most wonderful interfaces to application developers.

Links:

  1. PlayMorphia Model
  2. Advanced topics
  3. Home