简体   繁体   English

休眠的逆向工程列缺少不为空的属性

[英]hibernate reverse engineering column missing not null attribute

I am using hibernate reverse engineering to retrieve entities from DB (in this case oracle DB). 我正在使用休眠逆向工程从数据库(在本例中为oracle DB)检索实体。 I found in some cases constraints not-null is missing in generated hbm files. 我发现在某些情况下,生成的hbm文件中缺少约束not-null。 And it depends how table(column) is created(defined). 并且这取决于如何创建(定义)表(列)。

This is ok: 还行吧:

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

Then in hbm file we got for this column record: 然后在hbm文件中获得以下列记录:

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

This in not ok: 这不行:

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

Then in hbm file we got for this column record: 然后在hbm文件中获得以下列记录:

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

I don't understand why it is so, why hibernate is so sensitive to that? 我不明白为什么会这样,为什么冬眠如此敏感? Maybe is just matter of configuration, but I was not able found something related to that. 也许只是配置问题,但是我找不到与此相关的东西。

What is the problem? 问题是什么? Why in second case I missing "not-null=true" attribute ? 为什么在第二种情况下我缺少“ not-null = true”属性?

Thanks 谢谢

Write your own Strategy. 编写自己的策略。

Refere it in maven like this: 像这样在maven中引用它:

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

And write it like this: 并这样写:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM