我一直在尝试在junit测试中将spring和memorydb集成在一起,但我一直得到例外。
org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: ORDERTABLE
testcontext.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);
}
Order类:
@Entity
@Table(name="OrderTable")
public class Order {
@Id
@GeneratedValue
@Column(name="orderId")
String orderId;
PS:我的Order类有其他属性引用其他类,但我没有映射它们也不是实体。 任何建议
答案 0 :(得分:0)
关注this answer并了解您的XML映射,我建议将以下注释更改为orderId:
@Id
@GeneratedValue
@Column(name="orderId", columnDefintion="BIGINT")
String orderId;
假设@GeneratedValue
等同于XML中的“本机”生成器。您可能需要使用@GenericGenerator
代替策略= native而不是更多:
@Id
@GeneratedValue(generator="my-native-generator")
@GenericGenerator(name="my-native-generator", strategy = "native")
@Column(name="orderId", columnDefintion="BIGINT")
String orderId;