views:

635

answers:

1

Hi

I'm trying to configure Hibernate classes not through XML/Annotation, but using their programmatic API:

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

An example of column addition:

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

This works, till the first time I need to add a column with a name that already exists. Its not that I add the same column to the same table, these are two different tables, but nevertheless, I receive

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

I checked against the source code (I'm using Hibernate 3.3.1 GA) and there is a line in PersistentClass, line 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() );
 }
}

Did anybody try to configure it like that, had the same issue?...

Thanks in advance

+1  A: 

Your null pointer is because you haven't given your RootClass object an Entity name - you just need to call setEntityName on the root class and you will get past the initial exception.

You also need to define an identifier value on the root class - just call setIdentifier using the value that you want to make your identifier. (Don't also call addProperty with this or it will complain of column duplication).

Rachel Cullen