抛出QuerySyntaxException表的HibernatePagingItemReader未映射

时间:2017-08-28 05:46:04

标签: spring hibernate spring-boot spring-batch batch-processing

我正在开发使用spring-boot,spring-batch应用程序,我正在使用HibernatePagingItemReader从源表读取数据。当我运行代码时,我遇到异常。以下是我的应用程序代码。请注意,我在数据库中的表名是" OLDUSER"我的实体名称是" Olduser"。我已经使用了stackoverflow上针对类似问题提出的所有可能的建议,但是没有一个对我有用。 谢谢您的帮助。

    org.hibernate.hql.internal.ast.QuerySyntaxException: Olduser is not mapped [FROM Olduser o where o.id>=7501 and o.id <= 15000 order by o.id ASC]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)

域类:Olduser.java: -

    import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="OLDUSER")
@Entity
public class Olduser {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID")
    int id;
    @Column(name="USERNAME")
    String username;
    @Column(name="PASSWORD")
    String password;
    @Column(name="AGE")
    int age;

    public Olduser(int id, String username, String password, int age) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }

ItemReader代码: -

        @Bean
@StepScope
public HibernatePagingItemReader<Olduser> hibernateUserItemReader(
        @Value("#{stepExecutionContext[fromId]}") final String fromId,
        @Value("#{stepExecutionContext[toId]}") final String toId,
        @Value("#{stepExecutionContext[name]}") final String name) throws Exception {

    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    factoryBean.setDataSource(this.dataSource);
    factoryBean.afterPropertiesSet();
    SessionFactory sessionFactory = factoryBean.getObject();

    HibernatePagingItemReader<Olduser> hibernateReader=new HibernatePagingItemReader<>();
    hibernateReader.setFetchSize(chunkSize);
    hibernateReader.setQueryString("FROM Olduser o where o.id>=" + fromId + " and o.id <= " + toId +" order by o.id ASC");
    hibernateReader.setSessionFactory(sessionFactory);
    hibernateReader.setUseStatelessSession(false);
    hibernateReader.setSaveState(false);
    hibernateReader.afterPropertiesSet();
    return hibernateReader;
}

1 个答案:

答案 0 :(得分:0)

我找到了上述解决方案。我错过了在LocalSessionFactoryBean中添加annotatedpackages,这是上下文无法找到Entity类的原因。另外,我错过了管理事务的transactionManager类。以下是代码段。

创建了一个单独的bean sessionFactory并在hibernatePagingItemReader中添加了相同的

    hibernateReader.setSessionFactory(sessionFactory().getObject());

SessionFactory Bean: -

    @Bean
public LocalSessionFactoryBean sessionFactory() throws IOException{
    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    factoryBean.setDataSource(this.dataSource);
    factoryBean.setPackagesToScan("package name");
    factoryBean.afterPropertiesSet();
    return factoryBean;
}

TransactionManager bean: -

     @Bean
public JpaTransactionManager transactionManager() {
    return new JpaTransactionManager();
}