我编写了一个api,它使用spring-boot和hibernate从本地托管的数据库中读取数据,目前它只使用内置的会话工厂。计划是使用HSQL将此数据库移动到内存中我使用spring xml表示法实现了这一点:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:import.sql" />
</jdbc:embedded-database>
使用配置bean如下:
@Configuration
public class hsqlConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:import.sql")
.build();
}
}
系统的第一个查询工作正常 - AS下面:
public List<SectionAnswerBean> getSectionAnswer(List<Integer> answerIds, List<Integer> sectionIds) {
//SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "SELECT A.id AS answerId, A.sectionId AS sectionId, A.questionId AS questionId " +
"FROM Answer A " +
"WHERE id IN (:answers) " +
"AND A.sectionId IN (:sections)";
Query q = session.createQuery(hql)
.setResultTransformer(Transformers.aliasToBean(SectionAnswerBean.class));
q.setParameterList("answers", answerIds);
q.setParameterList("sections", sectionIds);
List<SectionAnswerBean> sectionAnswer = q.list();
session.close();
return sectionAnswer;
}
但是(下面)的第二个查询总是返回0:
public List<SectionRequirementBean> getSectionRequirementForAnswer(List<Integer> answerIds) {
//SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = getMySessionFactory().openSession();
String hql = "SELECT ARM.requirementId AS requirementId, A.sectionId AS sectionId " +
"FROM AnswerRequirementMapping ARM, Answer A " +
"WHERE ARM.answerId IN (:answers) " +
"AND ARM.answerId = A.id";
Query q = session.createQuery(hql)
.setResultTransformer(Transformers.aliasToBean(SectionRequirementBean.class));
q.setParameterList("answers", answerIds);
List<SectionRequirementBean> sectionRequirments = q.list();
return sectionRequirments;
}
似乎要清除数据库,我相信我在会话工厂自动接线到数据访问对象的方式有问题,这可以在下面看到:
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public SessionFactory getMySessionFactory() {
return sessionFactory;
}
public void setMySessionFactory(SessionFactory mySessionFactory) {
this.sessionFactory = sessionFactory;
}
有人对我在哪里出错有任何提示吗?