Play Framework: Model Development

Model development is one aspect of MVC that I do not have a lot of real world experience. Sure I am familiar with using an instance of a data model to work with Wicket. So developing the warehouse and book exchange data models was a great refresher.

Java Persistence
Coding the persistence with respect to the Ebean ORM was a fairly painless process compared to using the Hibernate ORM. Both ORMs used the same Java persistence annotations, yet Hibernate and Ebean differed on model retrieval. Hibernate needs each model to be associated with an entity manager instance. Although associating each model with an entity manager may be such a trivial step when creating a new project, moving an existing project over to the Hibernate ORM means that I need to sacrifice time to manage each model’s association. On the other hand, Ebean does not have an entity manager. Just creating the model with the persistence annotations means the model is ready to use. No more Hibernate configurations!

Warehouse Model

The warehouse model example helped jump start the programming with the various relationship persistence tags. I found that writing the tests was helpful as it gave me a sterile environment to understand how the models interact with regards to the persistentce database.

Textbook Exchange Model
TextbookExchange (2)

My first rendition of the textbook exchange request and offer models referenced the foreign key for both the student and the book models. Basically I still thought in term of database relations rather than persistence relations. After creating the models in Java, it quickly became apparent that the foreign keys were unnecessary and that I had to replicate the warehouse stockitem relation from the warehouse example.

Textbook Exchange: ERROR executing DML bindLog[] error

[textbookexchange] $ test
[info] Compiling 1 Java source to D:\workspace\textbookexchange\target\scala-2.1
[error] Test models.ModelTest.testModel failed: ERROR executing DML bindLog[] er
ror[Referential integrity constraint violation: "FK_REQUEST_STUDENT_3: PUBLIC.RE
)"; SQL statement:\n insert into request (primary_key, price, condition, student
_primary_key, book_primary_key) values (?,?,?,?,?) [23506-168]]
[error]     at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.execu
[error]     at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.inser
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersistExecute.exec
[error]     at com.avaje.ebeaninternal.server.core.PersistRequestBean.executeNow
[error]     at com.avaje.ebeaninternal.server.core.PersistRequestBean.executeOrQ
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersister.insert(De
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersister.saveEnhan
[error]     at com.avaje.ebeaninternal.server.persist.DefaultPersister.saveRecur
[error]     at
[error]     at
[error]     at
[error]     at
[error]     at
[error]     at models.ModelTest.testModel(
[error]     ...
[error] Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constrain
[error] insert into request (primary_key, price, condition, student_primary_key,
 book_primary_key) values (?,?,?,?,?) [23506-168]
[error]     at org.h2.message.DbException.getJdbcSQLException(
[error]     at org.h2.message.DbException.get(
[error]     at org.h2.message.DbException.get(
[error]     at org.h2.constraint.ConstraintReferential.checkRowOwnTable(Constrai
[error]     at org.h2.constraint.ConstraintReferential.checkRow(ConstraintRefere
[error]     at org.h2.table.Table.fireConstraints(
[error]     at org.h2.table.Table.fireAfterRow(
[error]     at org.h2.command.dml.Insert.insertRows(
[error]     at org.h2.command.dml.Insert.update(
[error]     at org.h2.command.CommandContainer.update(
[error]     at org.h2.command.Command.executeUpdate(
[error]     at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPrepa
[error]     at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedState
[error]     at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedS
[error]     at com.avaje.ebeaninternal.server.type.DataBind.executeUpdate(DataBi
[error]     at com.avaje.ebeaninternal.server.persist.dml.InsertHandler.execute(
[error]     at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.execu
[error]     ... 46 more
[info] models.ModelTest
[info] x models.ModelTest.testModel
[info] Total for test models.ModelTest
[info] Finished in 0.003 seconds
[info] 1 tests, 1 failures, 0 errors
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0
[error] Failed tests:
[error]         models.ModelTest
[error] (test:test) Tests unsuccessful
[error] Total time: 8 s, completed Apr 10, 2013 9:59:10 PM
[textbookexchange] $

In class we discussed a solution to this problem by adding getter and setter methods to each model’s attributes. I will update my code later if a more ideal solution does not appear.

Walk Through with Christopher Foo
There were a couple things I learned when Christopher walked me through his code and diagrams of his Warehouse and Textbook exchange models. We discussed the mapped-by persistence configuration since we both did not fully understand its usage.

Our textbook exchange model slightly differed where I assumed that a student to request is one to zero or many relation. Same went for the student to offer relation.

After Thoughts
Beyond the foreign key error with the Ebean ORM, I found this experience deepen my understanding of the Persistence API. Previously, I used Hibernate ORM only to store the results from execute stored procedures, so annotations such as @ManyToOne were left unused as all the relations were done in the stored procedure. Even with a greater understanding of the persistence API, I still feel that persistence is not an ideal solution to interacting with a database.