CQS pattern with Spring / Hibernate

804 views Asked by At

I'm developing a Java/Spring/Hibernate application that adheres to the CQS (Query Command Separation) pattern.

Put simply:

  • our domain model is only used by Commands which describe some operation that needs to be done on the domain model;
  • all read operations are done by a QueryService that bypasses the domain model so it can optimise (using projection) each query on a use case basis.


The domain model maps to MySQL database by using Hibernate.

I know there's a longrunning debate about separation between domain and persistence model. However, I'm convinced that nowadays Hibernate has become so flexible that you don't have to make hard sacrificies on the domain model. This way, you don't get a one-to-one mapping between domain and persistence model. Additionally, all DB-related stuff can be abstracted away by using Layer Supertypes.


My questions:

  1. If I use Hibernate Query (or Criteria) using projection in my QueryService, I'm in fact using my domain model instead of plain SQL. Isn't this a breach against the CQS pattern...?
  2. I could opt for using plain SQL in my QueryService. Is there any good SQL framework out there that gives me the possibility to build Queries without using domain model AND easily binding results into JavaBeans? The results are typically graphs, so it would be nice to easily bind these to some nested JavaBean structure.
0

There are 0 answers