我一直在尝试在junit测试中集成spring和memorydb,但我不断收到异常。
org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: ORDERTABLE
测试上下文.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<jdbc:embedded-database id="testDataSource" type="HSQL">
</jdbc:embedded-database>
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.bidblaze.service.test"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
<!-- Hibernate session factory -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource">
<ref bean="testDataSource" />
</property>
<property name="packagesToScan" value= MYPACKAGE />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.url">jdbc:hsqldb:mem:projectdb</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="SessionFactory" ref="SessionFactory" />
</bean>
定义其他bean
我的服务等级:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/testContext.xml" })
@Transactional
public class TestService {
//ApplicationContext context = new ClassPathXmlApplicationContext(
//"spring-config.xml");
@Autowired
PaypalPaymentsService PaypalService;
@Autowired
private OrderDAO orderDAO ;
@Autowired
private SessionFactory sessionFactory;
private Session extSession;
@Before
@Transactional
public void openSessionExternal() {
extSession = sessionFactory.getCurrentSession();
}
@Test
@Transactional
public void shouldHaveASessionFactory() {
assertNotNull(sessionFactory);
}
@Test
@Transactional
public void shouldHaveASession() {
assertNotNull(extSession);
}
@Test
public void saveOrder(){
Order order = new Order();
order.setOrderId("1");
order.setRecieverEmail("enduser_biz@gmail.com");
order.setAmount((double)2.00);
extSession.save(order);
Order testOrder = (Order) extSession.createQuery("from OrderTable where orderId = ?").setParameter(0, "1").list().get(0);
assertNotNull(testOrder);
}
订单类:
@Entity
@Table(name="OrderTable")
public class Order {
@Id
@GeneratedValue
@Column(name="orderId")
String orderId;
PS:我的 Order 类具有引用其他类的其他属性,但我没有映射它们,它们也不是实体。有什么建议么