/design/sqm.adoc

https://github.com/hibernate/hibernate-orm · AsciiDoc · 55 lines · 32 code · 23 blank · 0 comment · 0 complexity · 2b79e25b9f52d281f042333b3fd864fe MD5 · raw file

  1. = SQM
  2. The Semantic Query Model (SQM) is Hibernate's representation of an HQL or Criteria query's semantic (meaning). This
  3. representation is modeled as an "abstract syntax tree" (AST) - meaning it is a structured tree of nodes where each node
  4. represents an atomic piece of the query. E.g. `SqmSelectClause` represents the query's select clause.
  5. `SqmSelectClause` is ultimately a collection of one or more `SqmSelection` references representing the individual
  6. selections to be returned from the query (called the domain results). Etc
  7. All of these details are handled by the `QuerySqmImpl` implementation of `Query`. This is what Hibernate
  8. uses for both HQL and Criteria queries.
  9. == The Tree
  10. SQM is an Abstract Syntax Tree (AST) describing a query from the perspective of the domain model. It uses the Hibernate
  11. domain model, which is Hibernate's extension to the JPA model. This model contains no relational mapping information -
  12. it simply describes the domain model in mostly Java terms although it does incorporate "classifications" of the type
  13. system. E.g. it understand that `Customer` is an entity, but contains no information about the tables, columns, etc.
  14. See the `design/type-system-domain.adoc` design doc. For details about this domain model
  15. The tree model is defined in the package `org.hibernate.query.sqm.tree`
  16. == Building an SQM - HQL
  17. `org.hibernate.query.hql.HqlTranslator#translate`
  18. == Building an SQM - Criteria
  19. `org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder`
  20. == Translating an SQM
  21. Generic support for walking over the SQM tree via the `org.hibernate.query.sqm.SemanticQueryWalker` contract.
  22. More specialized support specifically for translating the SQM into a SQL AST is also defined by
  23. `org.hibernate.query.sqm.sql.SqmToSqlAstConverter`.
  24. [source]
  25. ----
  26. final SessionFactoryImplementor sessionFactory = ...;
  27. final QueryEngine queryEngine = sessionFactory.getQueryEngine();
  28. final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
  29. final SqmSelectTranslator sqmConverter = sqmTranslatorFactory.createSelectTranslator( ... );
  30. final SqmSelectTranslation interpretation = sqmConverter.translate( sqm );
  31. ----
  32. See the document `design/sql-ast.adoc` for details about the SQL AST, including its execution.