我想将XML数据存储在带有注册XML模式文件的Oracle XMLType列中。 XML文件和XSD架构都是有效的,XML符合架构。为了让Hibernate使用XMLType,我使用了Hibernate映射Document-XMLType,可以在这里找到:
http://solecjj.blogspot.com/2011/02/hibernate-with-oracle-xmltype.html
我的Hibernate映射XML如下所示:
...
<hibernate-mapping>
<class name="cz.zcu.kiv.eegdatabase.data.pojo.ScenarioType1" schema="JPERGLER" table="SCENARIO_TABLE_1">
<id name="scenarioId" type="int">
<column name="SCENARIO_ID" precision="22" scale="0"/>
<generator class="increment"/>
</id>
<property name="scenarioXml" type="cz.zcu.kiv.eegdatabase.data.datatypes.OracleXMLType">
<column name="SCENARIO_XML"/>
</property>
</class>
</hibernate-mapping>
这是相应的POJO类:
public ScenarioType1() {
}
public ScenarioType1(int scenarioId, Document scenarioXml) {
this.scenarioId = scenarioId;
this.scenarioXml = scenarioXml;
}
public int getScenarioId() {
return scenarioId;
}
private void setScenarioId(int scenarioId) {
this.scenarioId = scenarioId;
}
public Document getScenarioXml() {
return scenarioXml;
}
public void setScenarioXml(Document scenarioXml) {
this.scenarioXml = scenarioXml;
}
文档对象在控制器类中创建,并作为其POJO对象的属性移交给DAO对象:
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException bindException)
throws Exception {
MultipartFile xmlFile = data.getDataFileXml();
ScenarioType1 scenarioType1;
scenarioType1 = new ScenarioType1();
...
if ((xmlFile != null) && (!xmlFile.isEmpty())) {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
InputStream inputStream = xmlFile.getInputStream();
Document doc = docBuilder.parse(inputStream);
scenarioType1.setScenarioXml(doc);
inputStream.close();
}
scenarioTypeDao.create(scenarioType1);
...
}
DAO类和接口非常简单:
public interface ScenarioTypeDao extends GenericDao<ScenarioType1, Integer> {
}
public class SimpleScenarioTypeDao extends SimpleGenericDao<ScenarioType1, Integer>
implements ScenarioTypeDao {
public SimpleScenarioTypeDao() {
super(ScenarioType1.class);
}
}
当处理控制器类中的onSubmit()方法时,我收到以下错误消息:
Hibernate:插入JPERGLER.SCENARIO_TABLE_1(SCENARIO_XML,SCENARIO_ID)值(?,?)
SEVERE:servlet调度程序的Servlet.service()引发了异常 org.springframework.dao.CleanupFailureDataAccessException: 在关闭之前无法刷新会话:无法执行JDBC批量更新; 嵌套异常是org.hibernate.exception.GenericJDBCException: 无法执行JDBC批量更新
...
引起: org.hibernate.exception.GenericJDBCException:无法执行JDBC批量更新 在org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126) 在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114) 在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
...
引起: java.sql.BatchUpdateException: ORA-31011:XML解析失败
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:566) at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:9365) at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:210) 在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
看起来创建的Document对象填充了正确的数据,因此错误似乎发生在Hibernate映射端。我开始感到绝望,任何帮助都会受到赞赏。
这是我试图插入的示例XML文件:
<?xml version="1.0"?>
<scenarios>
<scenario name="P300" src="p300.xml"/>
<scenario src="070608_p300.xml" name="070608_p300" />
<scenario src="cisla_070608.xml" name="cisla_070608" />
</scenarios>