我最近从 hibernate3 升级到 hibernate5 并且我有一段代码addDocument(org.w3c.dom.Document doc)
,其中使用了已弃用的方法。我想用 , 或 替换它addUrl
,addResource
但addFile
我不知道该怎么做。
public MyHibernateMapping getHibernateConfiguration(final String tableName, final ColumnMetaInfo[] columnMetaInfos, final DatabaseType dbType, SynchronizationResult messages) throws ParserConfigurationException {
if(tableName == null) {
throw new IllegalArgumentException("parameter 'tableName' must not be null"); //$NON-NLS-1$
}
final MyHibernateMapping result = new MyHibernateMapping();
result.mColumnMapping = new HashMap<>();
final Configuration configuration = new Configuration();
configuration.setProperty(Environment.DIALECT, ScaHibernateHelper.getDialect(dbType));
configuration.setProperty(Environment.SHOW_SQL, "false"); //$NON-NLS-1$
final Document hibernateMappingDocument = XmlWriter.createDocument();
final Element hibernateMapping = XmlWriter.appendElement(hibernateMappingDocument, "hibernate-mapping"); //$NON-NLS-1$
hibernateMapping.setAttribute("default-access", MapAccessor.class.getName()); //$NON-NLS-1$
final Element classMapping = XmlWriter.appendElement(hibernateMapping, "class"); //$NON-NLS-1$
classMapping.setAttribute("name", java.util.HashMap.class.getName()); //$NON-NLS-1$
classMapping.setAttribute("table", tableName); //$NON-NLS-1$
boolean hasPrimaryKey = false;
boolean hasCompositeKey = hasCompositeKey(columnMetaInfos);
//first add the key
if(hasCompositeKey)
{
hasPrimaryKey = true;
final Element compositeId = XmlWriter.appendElement(classMapping, "composite-id"); //$NON-NLS-1$
for (final ColumnMetaInfo columnMetaInfo : columnMetaInfos) {
if(columnMetaInfo.isPrimaryKey())
{
final Element key = XmlWriter.appendElement(compositeId, "key-property"); //$NON-NLS-1$
final String quotedName = getMappedProperty(key, columnMetaInfo, messages);
result.mColumnMapping.put(columnMetaInfo.getName(), quotedName);
}
}
}
else
{
for (final ColumnMetaInfo columnMetaInfo : columnMetaInfos) {
if(columnMetaInfo.isPrimaryKey())
{
final Element id = XmlWriter.appendElement(classMapping, "id"); //$NON-NLS-1$
final String quotedName = getMappedProperty(id, columnMetaInfo, messages);
result.mColumnMapping.put(columnMetaInfo.getName(), quotedName);
hasPrimaryKey = true;
break;
}
}
}
for (ColumnMetaInfo columnMetaInfo2 : columnMetaInfos) {
ColumnMetaInfo columnMetaInfo = columnMetaInfo2;
final Element property;
if(!columnMetaInfo.isPrimaryKey())
{
property = XmlWriter.appendElement(classMapping, "property"); //$NON-NLS-1$
final String quotedName = getMappedProperty(property, columnMetaInfo, messages);
result.mColumnMapping.put(columnMetaInfo.getName(), quotedName);
}
}
if(!hasPrimaryKey)
{
final Element property = XmlWriter.appendElement(classMapping, "id"); //$NON-NLS-1$
property.setAttribute("name", PRIMARY_KEY_NAME); //$NON-NLS-1$
property.setAttribute("column", "id_pk"); //$NON-NLS-1$ //$NON-NLS-2$
property.setAttribute("type", "long"); //$NON-NLS-1$ //$NON-NLS-2$
result.mUseGeneratedKey = true;
}
// create a additional timestamp column that will be used during the synchronization mechanism
final Element property = XmlWriter.appendElement(classMapping, "property"); //$NON-NLS-1$
property.setAttribute("name", SYNCHRONIZED_ON); //$NON-NLS-1$
property.setAttribute("column", SYNCHRONIZED_ON); //$NON-NLS-1$
property.setAttribute("type", "timestamp"); //$NON-NLS-1$ //$NON-NLS-2$
configuration.addDocument(hibernateMappingDocument); //this should be replaced with other method
result.mConfiguration = configuration;
return result;}
所以这是我的方法,而不是 configuration.addDocument(hibernateMappingDocument),我想使用 addUrl 或 addResource 或 addFile 之类的其他东西。