Apache Torque 7.0 介绍与选型指南

本文介绍 Apache ORM 工具 Torque 7.0:Schema 驱动的生成器架构、Generator/Runtime 双模块、官方教程上手路径、适用限制,并与 Hibernate ORM、MyBatis 做许可、stars、发版节奏与兼容矩阵选型对比。

Apache TorqueApache Torque2026/10/18

Apache Torque 7.0 介绍与选型指南

一、信息基准

本次溯源的查询日期为 2026-10-18,信息全部取自 Apache 官方站点与 Maven Central 元数据,未做本地安装或运行验证。

版本基准:Apache Torque 7.0(官方下载页当前发行版、Maven Central latest/release 一致)。

项目内容
软件名称Apache Torque(db-torque)
当前版本7.0
版本核验Maven Central org.apache.torque:torque、torque-generator、torque-runtime 的 latest/release 均为 7.0,元数据更新时间 2025-08-13;官方下载页列出 Torque 7.0 二进制发行物
官方文档db.apache.org/torque/ 自动跳转至 torque-7.1 文档树(版本标识 7.1-SNAPSHOT,页面生成于 2025-09-10),即当前开发文档
项目状态ASF 活跃项目(非 Attic 退役项目;ASF 官方项目数据无 retired 标记)
开源许可Apache License 2.0
代码托管Apache GitBox(GitHub 无官方同步的主仓库,stars 官方未公布)

官方来源清单:

来源用途地址
Apache Torque 官网项目入口与文档跳转https://db.apache.org/torque/
官方下载页当前发行版本与二进制/源码包https://db.apache.org/torque/torque-7.1/download.html
ORM 参考文档Schema 定义、生成器运行、运行时初始化https://db.apache.org/torque/torque-7.1/documentation/orm-reference/running-the-generator.html
模块文档Generator/Runtime 模块、Templates、Maven Plugin、Ant Taskshttps://db.apache.org/torque/torque-7.1/documentation/modules/generator/index.html
Maven Central 元数据版本序列交叉核验https://repo1.maven.org/maven2/org/apache/torque/torque/maven-metadata.xml
ASF Attic 名单退役状态排除核查https://attic.apache.org/projects/

版本轨迹(Maven Central 元数据):3.3 → 4.0 → 5.0 → 5.1 → 6.0 → 7.0。官方新闻页最早可见的近期条目与既有生态资料表明 4.0/4.0.1 属于 2017—2018 年代版本;7.0 为当前最新发行版,7.1 处于文档与开发阶段(7.1-SNAPSHOT)。

二、软件概述

本次介绍的是 Apache Torque 7.0。Torque 是 ASF 旗下的 Java 对象关系映射(ORM)工具,其最大特点是"以代码生成器为中心":开发者先用 XML(schema 文件)描述数据库结构,再由 Torque Generator 生成数据对象(DO)与 Peer 类以及建表 SQL 脚本,最后由轻量的 Torque Runtime 配合生成代码完成数据库读写。

与运行期反射式 ORM 不同,Torque 将大部分映射工作前移到编译期/构建期,运行时组件只承担连接管理、SQL 生成与对象持久化等职责。这一设计让生成后的代码直观可读、可自行扩展,也让 Torque 在需要"数据库结构即代码资产"的团队中保有稳定位置。

Torque 隶属于 ASF 的 db 项目群(官网为 db.apache.org/torque),采用 Apache License 2.0,截至 2026-10-18 仍在演进:最新发行版 7.0 于 2025-08 月发布至 Maven Central,官方文档树已切换到 7.1-SNAPSHOT,说明 7.1 正在开发中。

三、核心架构

3.1 生成器(Torque Generator)

生成器模块负责把 XML Schema 描述转换为 Java 类与 SQL 脚本。官方模块文档显示 Generator 支持通过 Templates(模板)、Maven Plugin 与 Ant Tasks 三种途径接入构建流程;代码生成参考文档覆盖 Concepts、Configuration、Extending、Target Modes 等主题,支持自定义生成目标与模板。

3.2 Schema 描述(Defining the Schema)

Torque 以项目自有 XSD 约束的 XML 文件描述数据库:database、table、column、foreign-key、index 等元素构成模型定义。官方 ORM 参考的 Source XSD(strict)章节给出了严格的校验模式,Migration from Torque 3 章节则照顾从 3.x 迁移的项目。

3.3 运行时(Torque Runtime)

Runtime 模块负责与数据库交互:初始化(读取配置建立 Torque 实例与连接池)、读写数据库、连接与事务(Connections and Transactions)、异常处理等。官方 ORM 文档按"Initialisation of the Runtime → Reading from the DB → Writing to the DB"的顺序组织使用路径,并以独立章节描述 Extending classes、Inheritance、Managers and Cache、Beans 等进阶能力。

3.4 数据库独立性

官方文档设有 Database independence 与 Supported databases 章节:由 Schema 驱动生成面向目标数据库的 SQL,降低应用与特定数据库的耦合;对未内置支持的数据库,官方提供 Supporting a new DB 指南。

四、功能特性

特性说明与官方出处
XML Schema 驱动建模以 XSD 约束的 schema 文件定义库表结构(官方 Defining the Schema / Source XSD 章节)
代码生成由 Schema 生成数据对象与 Peer 类,支持模板定制与目标扩展(官方 Running the Generator / Code Generation Reference)
SQL 脚本生成生成建表脚本,服务于初始化与数据库独立性目标(官方 Database independence 章节)
Maven/Ant 双构建接入Generator 以 Maven Plugin 与 Ant Tasks 形式嵌入构建(官方模块文档)
运行时持久化连接与事务管理、读写数据库、异常处理(官方 ORM 文档对应章节)
对象模型增强继承映射、Manager 与缓存、Bean 导出、类扩展(官方 Extending classes / Inheritance / Managers and Cache / Beans 章节)
数据库可插拔内置支持若干数据库并开放新数据库适配指南(官方 Supported databases / Supporting a new DB)

五、快速上手

以下路径整理自 Torque 7.1 官方教程(ORM Tutorial,https://db.apache.org/torque/torque-7.1/documentation/tutorial/orm/index.html)与 ORM 参考文档,本文不做本地实测:

  1. 引入构件:在 Maven 构建中声明 org.apache.torque 下的 torque-generator、torque-runtime 等构件(版本 7.0,见 Maven Central 元数据)。
  2. 定义 Schema:按官方 Defining the Schema 章节编写描述库表的 XML 文件(元素形态示意如下,具体以官方 XSD 为准):
<database name="bookstore">
  <table name="book">
    <column name="id" required="true" primaryKey="true" type="INTEGER"/>
    <column name="title" type="VARCHAR" size="255"/>
  </table>
</database>
  1. 运行生成器:通过 Maven Plugin 或 Ant Task 执行生成(官方 Running the Generator 章节),产出数据对象、Peer 类与建表 SQL。
  2. 初始化运行时:按 Initialisation of the Runtime 章节完成 Torque 初始化与数据源配置,随后即可用生成的 Peer 类读写数据库。

六、适用场景与限制

6.1 适用场景

  • 以数据库结构为中心的 Java 应用:希望用受版本管理的 XML Schema 生成可读的持久化代码与建表脚本。
  • 传统关系型数据库栈的存量系统:历史上基于 Torque 3.x/4.x 的项目可沿 5.0/5.1/6.0/7.0 的升级路径延续(官方提供 Migration from Torque 3 文档)。
  • 对运行时开销敏感、偏好"生成期静态映射"的团队:运行时组件职责小,生成代码可审计、可改。
  • 需要生成多数据库 DDL 脚本的工具链场景。

6.2 限制

  • 编程模型偏传统:Peer/DO 风格与当代主流 ORM(JPA 注解、DSL 查询等)差异明显,新团队学习资料以官方文档与既有实践为主,社区生态较小。
  • 社区规模有限:GitHub 无官方主仓库,stars 官方未公布;问题反馈依赖 ASF 邮件列表与 Issue 跟踪。
  • 发布节奏稳健但缓慢:从 6.0 到 7.0 的演进以年计,新特性需耐心等待。
  • 与 JPA 生态不互通:生成代码非 JPA 实体,若企业规范强制 JPA,Torque 不适用。

七、同类选型对比

对比对象选取 Java 持久化领域两个主流方案:Hibernate ORM 与 MyBatis。数据联网实查,查询日期 2026-10-18;stars 采集自 GitHub 仓库页面,发版信息来自 GitHub Releases 与 Maven Central。

维度Apache Torque 7.0Hibernate ORMMyBatis 3
开源许可Apache-2.0(ASF)Apache-2.0(hibernate-core 8.0.0.Beta3 POM 许可声明;早期版本线为 LGPL-2.1)Apache-2.0
GitHub stars(2026-10-18)官方未公布(无官方 GitHub 主仓库,代码在 Apache GitBox)6473(github.com/hibernate/hibernate-orm)20445(github.com/mybatis/mybatis-3)
最近发版7.0(Maven Central 元数据 2025-08-13)6.6.59(GitHub Releases,2026-10-18);8.0 已进入 Beta(Maven Central 8.0.0.Beta3)3.5.19(GitHub Releases,2025-01-02)
版本节奏大版本演进慢(6.0 → 7.0 以年计),文档树已到 7.1-SNAPSHOT双线高频发版:6.6.x 维护线与 7.x/8.0 新特性线并行3.5.x 长线维护,节奏平缓
映射范式XML Schema → 生成期生成 DO/Peer 与 SQL 脚本,运行时轻量JPA 标准实现,注解/XML 映射,运行时反射与状态管理,功能全面SQL 映射框架,手写 SQL + XML/注解映射,介于两者之间
兼容矩阵Java 构建链(Maven/Ant),支持多数据库适配Jakarta Persistence 生态,多数据库方言体系JDBC 之上的轻量框架,数据库无强耦合

数据来源:GitHub 仓库页面与 releases.atom、Maven Central 元数据(org.apache.torque、org.hibernate.orm:hibernate-core 8.0.0.Beta3 POM、org.mybatis:mybatis)、db.apache.org/torque 官方页面(2026-10-18 查询)。

选型建议:需要"数据库结构即代码资产"的生成期静态映射、且团队可接受较小社区时选 Apache Torque;遵循 JPA 标准、追求完整 ORM 能力与活跃生态选 Hibernate;以 SQL 掌控力优先、团队熟悉手写 SQL 则选 MyBatis。

八、参考链接

  1. Apache Torque 官网:https://db.apache.org/torque/
  2. 官方下载页(Torque 7.0):https://db.apache.org/torque/torque-7.1/download.html
  3. ORM 教程:https://db.apache.org/torque/torque-7.1/documentation/tutorial/orm/index.html
  4. Schema 定义参考:https://db.apache.org/torque/torque-7.1/documentation/orm-reference/defining-the-schema.html
  5. 生成器运行参考:https://db.apache.org/torque/torque-7.1/documentation/orm-reference/running-the-generator.html
  6. Generator 模块文档:https://db.apache.org/torque/torque-7.1/documentation/modules/generator/index.html
  7. Runtime 模块文档:https://db.apache.org/torque/torque-7.1/documentation/modules/runtime/index.html
  8. Maven Central 元数据:https://repo1.maven.org/maven2/org/apache/torque/torque/maven-metadata.xml
  9. ASF Attic 项目名单(退役核查):https://attic.apache.org/projects/
  10. Hibernate ORM 仓库:https://github.com/hibernate/hibernate-orm
  11. MyBatis 仓库:https://github.com/mybatis/mybatis-3