【问题标题】:How I can quote tables using hibernate3-maven-plugin hbm2ddl?如何使用 hibernate3-maven-plugin hbm2ddl 引用表?
【发布时间】:2011-04-19 15:54:01
【问题描述】:

我有一个使用 maven 构建的项目,我需要使用 hibernate3-maven-plugin 中的 hbm2ddl 工具生成一个模式。

我需要用一个名为 Order 的表创建数据库,就像 SQL 关键字一样,我不知道如何让 maven 在生成脚本时引用这个表。我已经进行了搜索,我发现在 hibernate 中有一个属性可以告诉 hbm2ddl 工具,但我不能告诉插件使用它:

<property name="hbm2ddl.keywords">auto-quote</property>

如果我不引用表格,hbm2ddl 会生成一个脚本:

create table Order (orderId varchar(36) not null, orderCode integer, customer varchar(36) not null, supplier varchar(36) not null, product varchar(36) not null, forecast float, dateRaised date not null, dateDispatched date, dateReceived date, quantityOrdered double precision not null, quantitySent double precision, primary key (orderId)) ENGINE=InnoDB;

无法编译(由于明显的语法错误):

02:51:41,264 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table Order (orderId varchar(36) not null, orderCode integer, customer varchar(36) not null, supplier varchar(36) not null, product varchar(36) not null, forecast float, dateRaised date not null, dateDispatched date, dateReceived date, quantityOrdered double precision not null, quantitySent double precision, primary key (orderId)) ENGINE=InnoDB
02:51:41,264 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order (orderId varchar(36) not null, orderCode integer, customer varchar(36) not' at line 1

这是 pom.xml 文件的一部分:

<configuration>
 <components>
  <component>
   <name>hbm2java</name>
   <implementation>annotationconfiguration</implementation>
   <outputDirectory>src/main/java</outputDirectory>
  </component>
  <component>
   <name>hbm2ddl</name>
   <implementation>annotationconfiguration</implementation>
   <outputDirectory>src/main/resources</outputDirectory>
  </component>
  <component>
   <name>hbm2doc</name>
   <implementation>annotationconfiguration</implementation>
   <outputDirectory>docs/html/hibernate</outputDirectory>
  </component>
 </components>
 <componentProperties>
  <create>true</create>
  <drop>true</drop>
  <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
  <propertyfile>src/main/resources/database.properties</propertyfile>
  <jdk5>true</jdk5>
  <outputfilename>amasbe_db.sql</outputfilename>
 </componentProperties>
</configuration>

非常感谢任何提示或帮助。

谢谢!

【问题讨论】:

    标签: hibernate maven-2 keyword hbm2ddl


    【解决方案1】:

    AFAIK,hbm2ddl.keywords 是 NHibernate 功能,Hibernate 不支持。

    使用 Hibernate,您必须自己引用名称:

    @Entity
    @Table(name="`Order`")
    public class Order {
        ...
    }
    

    文档的相关部分是:

    5.4. SQL quoted identifiers

    你可以强制 Hibernate 引用 生成的 SQL 中的标识符 将表或列名包含在 映射文档中的反引号。 Hibernate 将使用正确的 SQL 方言的引用样式。 这通常是双引号,但 SQL Server 使用括号和 MySQL 使用反引号。

    <class name="LineItem" table="`Line Item`">
        <id name="id" column="`Item Id`"/><generator class="assigned"/></id>
        <property name="itemNumber" column="`Item #`"/>
        ...
    </class>
    

    参考文献

    【讨论】:

    • 嗯,可惜在hibernate中不存在这样的功能,没有意义,但感谢您的工作! :)
    • @Bene 是的,我同意。您可能想跟踪(并投票支持)HHH-2578
    【解决方案2】:

    可以在此处找到该问题的另一种解决方案:https://forum.hibernate.org/viewtopic.php?p=2409922

    基本上,它只需要派生一个类来响应提供表名/列名。

    希望对您有所帮助。

    干杯, 长隆

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 2011-02-19
      • 2011-02-27
      • 1970-01-01
      • 2012-10-19
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多