簡體   English   中英

在內存中db休眠狀態

[英]in memory db hibernate spring

我一直在嘗試在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>

與定義其他豆類

我的服務等級:

                 @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類具有引用其他類的其他屬性,但我沒有映射它們,它們也不是實體。 有什么建議么

按照這個答案 ,並了解您的XML映射,我建議對orderId進行以下注釋更改:

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

假定@GeneratedValue等同於XML中的“本機”生成器。 您可能需要使用@GenericGenerator代替,而將strategy = native改為這樣:

@Id
@GeneratedValue(generator="my-native-generator")
@GenericGenerator(name="my-native-generator", strategy = "native")
@Column(name="orderId", columnDefintion="BIGINT")
String orderId;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM