【发布时间】: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() );
}
}
有没有人尝试过这样配置,遇到同样的问题?...
提前致谢
【问题讨论】: