hibernate逆向工程列缺少非null属性

时间:2014-04-25 13:16:43

标签: java hibernate reverse-engineering

我正在使用hibernate逆向工程从DB(在本例中为oracle DB)中检索实体。 我发现在某些情况下生成的hbm文件中缺少约束not-null。 它取决于如何创建(定义)表(列)。

没关系:

CREATE TABLE "MY_TABLE1"
(
...
"RECORD_CREATED" DATE DEFAULT SYSDATE NOT NULL ENABLE,
...

然后在hbm文件中我们得到了这个列记录:

<column length="7" name="RECORD_CREATED" not-null="true"></column>

这还不行:

CREATE TABLE "MY_TABLE1"
(
...
  "RECORD_CREATED" DATE DEFAULT SYSDATE,
  ...
  ...
  CHECK ("RECORD_CREATED" IS NOT NULL) ENABLE,
  ...

然后在hbm文件中我们得到了这个列记录:

<column length="7" name="RECORD_CREATED"></column>

我不明白为什么会这样,为什么hibernate对此非常敏感? 也许只是配置问题,但我找不到与之相关的东西。

有什么问题?为什么在第二种情况下我错过&#34; not-null = true&#34;属性?

由于

1 个答案:

答案 0 :(得分:0)

编写自己的策略。

像这样在maven中引用它:

<build><plugins><plugin>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <configuration>
      <components>
        <component>
          <name>hbm2java</name>
        </component>
      </components>
      <reversestrategy>MyStrategyImpl</reversestrategy>
         ...

然后这样写:

public class MyStrategyImpl extends DelegatingReverseEngineeringStrategy {
    public String columnToHibernateTypeName(TableIdentifier table, String columnName, int sqlType, int length, int precision, int scale, boolean nullable, boolean generatedIdentifier) {
      if(/*yourJdbcSelects*/){
        nullable = true;
      }
      super.columnToHibernateTypeName(table, columnName, sqlType, length, precision, scale, nullable, generatedIdentifier);
    }
}
相关问题