I typically use mybatis and stored procedure as my application/database bridge but I'm evaluating hibernate for a new project. There are some aspects of Hibernate that seem neat, but I'm not convinced it's going to solve more problems for me than it creates. So, please help me understand if I should pursue Hibernate for my application.
The hibernate documentation suggests that the Session object should be re-used for multiple database transactions that span a single 'unit of work'. However, my application will exist behind a stateless REST api and so, the Session can not persist between requests. It seems then the Session-Per-Request pattern must be used. Does this pattern work? Do you pay too much overhead for creating a Session for each CRUD operation that you use for one operation and then close? A typical workflow might be: 1) User issues a GET request for an object by ID. 2) Create a Session and load the object. 3) Close session, serialize the object to JSON and deliver in response. 4) User POSTs modifications to object with new JSON payload. 5) Deserialize JSON into an entity instance. 6) Create a new Session. 7) Start a transaction. 8) Persist the object. 7) Commit transaction and close the Session.
Is this the right way to leverage Hibernate?
Thanks!
|