Replacing a full ORM (JPA/Hibernate) by a lighter solution : Recommended patterns for load/save?

前端 未结 8 1685
孤城傲影
孤城傲影 2021-01-29 20:26

I\'m developing a new Java web application and I\'m exploring new ways (new for me!) to persist the data. I mostly have experience with JPA & Hibernate but, except for simpl

8条回答
  •  清酒与你
    2021-01-29 20:56

    Persistence Approaches

    The spectrum of solutions from simple/basic to sophisticated/rich is:

    • SQL/JDBC - hard-code SQL within objects
    • SQL-Based Framework (e.g. jOOQ, MyBatis) - Active Record Pattern (separate general object represents row data and handles SQL)
    • ORM-Framework (e.g. Hibernate, EclipseLink, DataNucleus) - Data Mapper Pattern (Object per Entity) plus Unit Of Work Pattern (Persistence Context / Entity Manager)

    You seek to implement one of the first two levels. That means shifting focus away from the object model towards SQL. But your question asks for Use Cases involving the object model being mapped to SQL (i.e. ORM behaviour). You wish to add functionality from the third level against functionality from one of the first two levels.

    We could try to implement this behaviour within an Active Record. But this would need rich metadata to be attached to each Active Record instance - the actual entity involved, it's relationships to other entities, the lazy-loading settings, the cascade update settings. This would make it effectively a mapped entity object in hiding. Besides, jOOQ and MyBatis don't do this for Use Cases 1 & 2.

    How To Achieve Your Requests?

    Implement narrow ORM behaviour directly into your objects, as a small custom layer on top of your framework or raw SQL/JDBC.

    Use Case 1: Store metadata for each entity object relationship: (i) whether relationship should be lazy-loaded (class-level) and (ii) whether lazy-load has occured (object-level). Then in the getter method, use these flags to determine whether to do lazy-load and actually do it.

    Use Case 2: Similar to Use Case 1 - do it yourself. Store a dirty flag within each entity. Against each entity object relationship, store a flag describing whether the save should be cascaded. Then when an entity is saved, recursively visit each "save cascade" relationship. Write any dirty entities discovered.

    Patterns

    • Lazy Load
    • Cascading Updates
    • Metadata Mapping
    • Unit of Work

    Pros

    • Calls to SQL framework are simple.

    Cons

    • Your objects become more complicated. Take a look at the code for Use Cases 1 & 2 within an open source product. It's not trivial
    • Lack of support for Object Model. If you're using object model in java for your domain, it will have lesser support for data operations.
    • Risk of scope creep & anti-patterns: the above missing functionality is the tip of the iceberg. May end up doing some Reinvent the Wheel & Infrastructure Bloat in Business Logic.
    • Education and Maintenance on non-standard solution. JPA, JDBC and SQL are standards. Other frameworks or custom solutions aren't.

    Worthwhile???

    This solution works well if you have fairly simple data handling requirements and a data model with a smaller number of entities:

    • If so, great! Do above.
    • If not, this solution's a poor fit and represents false savings in effort - i.e. will end up taking longer and being more complicated than using an ORM. In that case, have another look at JPA - it might be simpler than you think and it supports ORM for CRUD plus raw SQL for complicated queries :-).

提交回复
热议问题