Hibernate - programmatic configuration

I am trying to customize Hibernate classes not via XML / Annotation but using their programmatic API:

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

      

Example of adding a column:

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, before the first time I need to add a column with a name that already exists. It's not that I am adding the same column to the same table, they are two different tables, but still I get

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

      

I checked the source code (I am 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() );
    }
}

      

Has anyone tried to set it up like this, had the same problem? ...

Thank you in advance

+1


source to share


1 answer


Your null pointer is that you didn't cast your RootClass object to the Entity name - you just need to call setEntityName on the root class and you will get the previous exception.



You also need to define the ID value in the root class - just call setIdentifier using the value you want to make your ID. (Don't call addProperty with this or it will complain about duplicate columns).

+1


source







All Articles