0

我一直在尝试在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 类具有引用其他类的其他属性,但我没有映射它们,它们也不是实体。有什么建议么

4

1 回答 1

0

按照这个答案,并了解您的 XML 映射,我建议对 orderId 进行以下注释更改:

@Id
@GeneratedValue
@Column(name="orderId", columnDefintion="BIGINT")
String orderId;

假设这@GeneratedValue等同于您的 XML 中的“本机”生成器。您可能需要将 an@GenericGenerator与 strategy=native 一起使用,而不是像这样:

@Id
@GeneratedValue(generator="my-native-generator")
@GenericGenerator(name="my-native-generator", strategy = "native")
@Column(name="orderId", columnDefintion="BIGINT")
String orderId;
于 2013-08-20T15:10:51.760 回答