2012-11-20 48 views
3

編輯:儘管我認爲hibernate應該能夠自動執行此操作,但我有一個DDL腳本[包含我需要的結構。如果我可以運行這個腳本,讓休眠自動創建表格,我現在很高興......'see this question不是由Hibernate4創建的Spring批處理表

我爲這篇冗長的文章找藉口。我的問題,雖然可能很簡單,但作爲一個冬眠和春天noob,這是在這整個上午竊聽我...

我正在使用彈簧批。在運行導入作業時,Spring希望將關於作業執行的元數據存儲在名爲BATCH_JOB的表中。但是,這個表不是由Hibernate創建的。

我通過運行作業:

public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { 

     ApplicationContext context = new ClassPathXmlApplicationContext("bean_configuration.xml"); 
     SimpleJobLauncher launcher = (SimpleJobLauncher) context.getBean("jobLauncher"); 
     FlowJob job = (FlowJob) context.getBean("importIVSJob"); 
     launcher.run(job, new JobParameters()); 
    } 

我的休眠屬性文件:

#hibernate.c3p0.min_size=5 
#hibernate.c3p0.max_size=20 
#hibernate.c3p0.timeout=1800 
#hibernate.c3p0.max_statements=50 
hibernate.dialect=org.hibernate.dialect.HSQLDialect 
hibernate.show_sql=true 
hibernate.hbm2ddl.auto=create 

和XML配置:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:batch="http://www.springframework.org/schema/batch" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
        http://www.springframework.org/schema/batch 
        http://www.springframework.org/schema/batch/spring-batch-2.1.xsd"> 


     <batch:job job-repository="jobRepository" id="importIVSJob"> 
     <batch:step id="step1"> 
      <batch:tasklet transaction-manager="transactionManager"> 
       <batch:chunk reader="csvFileReader" writer="itemWriter" 
        commit-interval="5" /> 
      </batch:tasklet> 
     </batch:step> 
    </batch:job> 

    <bean id="csvFileReader" class="org.springframework.batch.item.file.FlatFileItemReader"> 
     <property name="resource" ref="inputResource" /> 
     <property name="lineMapper"> 
      <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> 
       <property name="lineTokenizer"> 
        <bean 
         class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" 
         p:delimiter=";" 
         p:names="objectcode, event, beginTijd, duurEvent, kolkNummer, deelkolkNummer, vaarrichting, eniNummer, aantalSchepen, scheepType, vaartType, subTypeVaart, rwsKlasseHoofgroep, rwsKlasseSubgroep, cemtKlasse, laadvermogen, dwtLaadvermogen, scheepslengte, scheepsbreedte, scheepsdiepgang, scheepshoogte, vlagCode, beladingsCode, cargoGewicht, seinVoeringKegel, vrachtAanBoord, aantalContainers, aantalTEU_containers, reisId, toerbeurt, invaartGroen, uitvaartGroen" /> 
       </property> 
       <property name="fieldSetMapper"> 
        <bean 
         class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper" 
         p:targetType="nl.triopsys.styx.importeer.IVS.IvsBericht" /> 
       </property> 
      </bean> 
     </property> 
    </bean> 

    <bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"> 
     <property name="resource" ref="outputResource" /> 
     <property name="lineAggregator"> 
      <bean 
       class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"> 
       <property name="delimiter" value="," /> 
       <property name="fieldExtractor"> 
        <bean 
         class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"> 
         <property name="names" 
          value="objectcode, event, beginTijd, duurEvent, kolkNummer, deelkolkNummer, vaarrichting, eniNummer, aantalSchepen, scheepType, vaartType, subTypeVaart, rwsKlasseHoofgroep, rwsKlasseSubgroep, cemtKlasse, laadvermogen, dwtLaadvermogen, scheepslengte, scheepsbreedte, scheepsdiepgang, scheepshoogte, vlagCode, beladingsCode, cargoGewicht, seinVoeringKegel, vrachtAanBoord, aantalContainers, aantalTEU_containers, reisId, toerbeurt, invaartGroen, uitvaartGroen" /> 
        </bean> 
       </property> 
      </bean> 
     </property> 
    </bean> 

    <bean id="inputResource" class="org.springframework.core.io.FileSystemResource"> 
     <constructor-arg index="0" value="file:C:\Users\jgoddijn\testdata\IVS90.csv" /> 
    </bean> 

    <bean id="outputResource" class="org.springframework.core.io.FileSystemResource"> 
     <constructor-arg index="0" value="C:\Users\jgoddijn\testdata\output.csv" /> 
    </bean> 

    <bean 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:location="batch.properties" p:ignoreUnresolvablePlaceholders="true" /> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
     <property name="url" 
      value="jdbc:hsqldb:file:/D:/DMVV/HSQLDB;shutdown=true;hsqldb.write_delay=false" /> 
     <property name="username" value="sa" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>nl.triopsys.styx.importeer.IVS.IvsBericht</value> 
       <value>nl.triopsys.styx.domain.Vaartuig</value> 
       <value>nl.triopsys.styx.domain.AIS.MMSI</value> 
       <value>nl.triopsys.styx.domain.IVS90.valuetype.Scheepsnummer</value> 
       <value>nl.triopsys.styx.domain.IVS90.valuetype.EniNummer</value> 
       <value>nl.triopsys.styx.domain.IVS90.valuetype.ImoNummer</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <bean 
       class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
       <property name="location" value="batch.properties" /> 
      </bean> 
     </property> 
    </bean> 

    <bean id="jobRegistry" 
     class="org.springframework.batch.core.configuration.support.MapJobRegistry" /> 

    <bean id="jobLauncher" 
     class="org.springframework.batch.core.launch.support.SimpleJobLauncher" 
     p:jobRepository-ref="jobRepository" /> 

    <bean id="jobRegistryBeanPostProcessor" 
     class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor" 
     p:jobRegistry-ref="jobRegistry" /> 

    <bean id="jobRepository" 
     class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean" 
     p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" /> 

</beans> 

和錯誤,我得到:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: BATCH_JOB_INSTANCE 
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:83) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
    at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:146) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
    at $Proxy20.prepareStatement(Unknown Source) 
    at org.springframework.jdbc.core.JdbcTemplate$SimplePreparedStatementCreator.createPreparedStatement(JdbcTemplate.java:1436) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:581) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674) 
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:714) 
    at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:204) 
    at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:209) 
    at org.springframework.batch.core.repository.dao.JdbcJobInstanceDao.getJobInstance(JdbcJobInstanceDao.java:221) 
    at org.springframework.batch.core.repository.support.SimpleJobRepository.getLastJobExecution(SimpleJobRepository.java:253) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at $Proxy19.getLastJobExecution(Unknown Source) 
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:94) 
    at nl.triopsys.styx.sandbox.importeer.RunImport.main(RunImport.java:39) 

似乎沒有爲作業執行創建表。如何讓休眠自動執行此操作?

謝謝!

回答

0

的最快方法是指定腳本的H2 DB URL的INITIALISE參數:

jdbc:h2:~/mydb;init=runscript from 'classpath:org/springframework/batch/core/schema-h2.sql'

提供彈簧一批覈心罐子在classpath會造成對這些表第一次運行你的測試。您可以在創建表格後將其刪除。