5.7. 辅助数据库对象(Auxiliary Database Objects)

Allows CREATE and DROP of arbitrary database objects, in conjunction with Hibernate's schema evolution tools, to provide the ability to fully define a user schema within the Hibernate mapping files. Although designed specifically for creating and dropping things like triggers or stored procedures, really any SQL command that can be run via a java.sql.Statement.execute() method is valid here (ALTERs, INSERTS, etc). There are essentially two modes for defining auxiliary database objects… 帮助CREATE和DROP任意数据库对象,与Hibernate的schema交互工具组合起来,可以提供在Hibernate映射文件中完全定义用户schema的能力。虽然这是为创建和销毁trigger(触发器)或stored procedure(存储过程)等特别设计的,实际上任何可以在java.sql.Statement.execute()方法中执行的SQL命令都可以在此使用(比如ALTER, INSERT,等等)。本质上有两种模式来定义辅助数据库对象…

第一种模式是在映射文件中显式声明CREATE和DROP命令:

  1. <hibernate-mapping>
  2. ...
  3. <database-object>
  4. <create>CREATE TRIGGER my_trigger ...</create>
  5. <drop>DROP TRIGGER my_trigger</drop>
  6. </database-object>
  7. </hibernate-mapping>

第二种模式是提供一个类,这个类知道如何组织CREATE和DROP命令。这个特别类必须实现org.hibernate.mapping.AuxiliaryDatabaseObject接口。

  1. <hibernate-mapping>
  2. ...
  3. <database-object>
  4. <definition class="MyTriggerDefinition"/>
  5. </database-object>
  6. </hibernate-mapping>

还有,这些数据库对象可以特别指定为仅在特定的方言中才使用。

  1. <hibernate-mapping>
  2. ...
  3. <database-object>
  4. <definition class="MyTriggerDefinition"/>
  5. <dialect-scope name="org.hibernate.dialect.Oracle9Dialect"/>
  6. <dialect-scope name="org.hibernate.dialect.OracleDialect"/>
  7. </database-object>
  8. </hibernate-mapping>