0
我一直在尝试在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("[email protected]");
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类有其他属性引用其他类,但我没有映射它们,它们也不是实体。 任何建议
望着那错误的下半场 - 没有[这个苏答案(http://stackoverflow.com/questions/ 13612021/hibernate-does-not-generate-table-with-annotations)help?你的Order类是否实现了Serializable? – n0741337
起初它没有,但现在我看到你的评论我让它实现了Serializable并试图运行junit测试,它仍然给出相同的异常 –
你有没有参数的构造函数?另一个答案是按照惯例放弃了表/列名,以支持命名。如果删除表和列注释并将字段更改为“id”(来自orderId),它是否工作?另外,你是否真的需要Order中的字符串id(而不是数字类型)? – n0741337