【问题标题】:Hibernate - Programmatic configurationHibernate - 程序化配置
【发布时间】:2010-09-24 15:05:57
【问题描述】:

我正在尝试不通过 XML/Annotation 配置 Hibernate 类,而是使用它们的编程 API:

Mappings mappings = configuration.createMappings();
    mappings.addClass(...);

列添加示例:

public void addColumn(String colName, String accessorName, NullableType type)
      {
        if(this._table == null)
          {
            return;
          }

        Column column = new Column(colName);
//        this._table.addColumn(column);

        Property prop = new Property();
        prop.setName(accessorName);

        SimpleValue simpleValue = new SimpleValue();
        simpleValue.setTypeName(type.getName());
        simpleValue.addColumn(column);
        simpleValue.setTable(_table);
        prop.setValue(simpleValue);

        this._rootClass.addProperty(prop);
      }

这有效,直到我第一次需要添加一个名称已经存在的列。不是我将同一列添加到同一个表中,这是两个不同的表,但是,我收到了

 ERROR:  java.lang.NullPointerException
    at
 org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:711)

我检查了源代码(我使用的是 Hibernate 3.3.1 GA)并且在 PersistentClass 中有一行,第 711 行:

protected void checkColumnDuplication() {
    HashSet cols = new HashSet(); <=========After this line 'cols' already contain data!
    if (getIdentifierMapper() == null ) {
        //an identifier mapper => getKey will be included in the getNonDuplicatedPropertyIterator()
        //and checked later, so it needs to be excluded
        checkColumnDuplication( cols, getKey().getColumnIterator() );
    }
    checkColumnDuplication( cols, getDiscriminatorColumnIterator() );
    checkPropertyColumnDuplication( cols, getNonDuplicatedPropertyIterator() );
    Iterator iter = getJoinIterator();
    while ( iter.hasNext() ) {
        cols.clear();
        Join join = (Join) iter.next();
        checkColumnDuplication( cols, join.getKey().getColumnIterator() );
        checkPropertyColumnDuplication( cols, join.getPropertyIterator() );
    }
}

有没有人尝试过这样配置,遇到同样的问题?...

提前致谢

【问题讨论】:

    标签: hibernate configuration


    【解决方案1】:

    你的空指针是因为你没有给你的 RootClass 对象一个实体名称 - 你只需要在根类上调用 setEntityName ,你就会摆脱最初的异常。

    您还需要在根类上定义一个标识符值 - 只需使用您想要创建标识符的值调用 setIdentifier。 (不要也用这个调用 addProperty 否则它会抱怨列重复)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2019-07-15
      • 2023-03-12
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多