2

Maven で構築しているプロジェクトがあり、hibernate3-maven-plugin の hbm2ddl ツールを使用してスキーマを生成する必要があります。

SQL キーワードのようなOrderというテーブルを使用してデータベースを作成する必要がありますが、スクリプトを生成するときにこのテーブルを引用するように maven を作成する方法がわかりません。検索を行ったところ、hbm2ddl ツールにこれを指示するプロパティが hibernate にあることがわかりましたが、プラグインにそれを使用するように指示することはできません。

<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>

ヒントやヘルプは本当にありがたいです。

ありがとうございました!

4

2 に答える 2

4

私の知る限り、これhbm2ddl.keywordsはNHibernateの機能であり、Hibernateではサポートされていません。

Hibernate では、名前を自分で引用する必要があります。

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

ドキュメントの関連セクションは次のとおりです。

5.4. SQL 引用識別子

マッピングドキュメントでテーブルまたはカラム名をバッククォートで囲むことにより、生成された SQL で Hibernate に識別子を引用させることができます。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>

参考文献

于 2010-10-01T05:40:31.403 に答える
0

この問題の別の解決策は、https ://forum.hibernate.org/viewtopic.php?p=2409922 にあります。

基本的に、必要なのは、テーブル名/列名を提供するために応答するクラスを派生させることだけです。

それが役立つことを願っています。

乾杯、チュオン

于 2011-09-24T17:38:32.607 に答える