Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 每次运行单元测试时,是什么在擦除H2数据库?_Spring_Hibernate_Jpa_H2 - Fatal编程技术网

Spring 每次运行单元测试时,是什么在擦除H2数据库?

Spring 每次运行单元测试时,是什么在擦除H2数据库?,spring,hibernate,jpa,h2,Spring,Hibernate,Jpa,H2,我有一个Spring+Hibernate+H2项目,我是根据我在互联网上找到的一个例子制作的。它工作得很好,只是每次我运行单元测试时,数据库都会被删除。我不确定是什么引起的。测试通过得很好,但在测试运行后,我在测试之前在db中输入的任何内容都将被清除 任何想法都会有帮助!谢谢 这是我的infrastructure.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework

我有一个Spring+Hibernate+H2项目,我是根据我在互联网上找到的一个例子制作的。它工作得很好,只是每次我运行单元测试时,数据库都会被删除。我不确定是什么引起的。测试通过得很好,但在测试运行后,我在测试之前在db中输入的任何内容都将被清除

任何想法都会有帮助!谢谢

这是我的infrastructure.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc     
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                    http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="booksrus" />

</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="dataSource" 
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="org.h2.Driver"/>
  <property name="url" value="jdbc:h2:tcp://localhost:9092/mydb"/>
  <property name="username" value=""/>
  <property name="password" value=""/>

这是属性name=“hibernate.hbm2ddl.auto”value=“create”/> 这意味着每次重新创建模式时都会删除n。 将其更改为update,这样它将仅在第一次创建它(如果不存在)

有关详细说明,请参阅此链接:

这是属性name=“hibernate.hbm2ddl.auto”value=“create”/> 这意味着每次重新创建模式时都会删除n。 将其更改为update,这样它将仅在第一次创建它(如果不存在)

有关详细说明,请参阅此链接:

我按照您的建议将其更改为更新,它停止清除数据库。然而,在测试之后,我持久化的实体并没有在数据库中结束。我使用相同的代码在一个单独的类中运行,并使用一个main方法,实体显示出来。我猜@RunWith(SpringJUnit4ClassRunner.class)会在测试结束时强制回滚对db的更改。谢谢你的帮助!我按照您的建议将其更改为更新,它停止清除数据库。然而,在测试之后,我持久化的实体并没有在数据库中结束。我使用相同的代码在一个单独的类中运行,并使用一个main方法,实体显示出来。我猜@RunWith(SpringJUnit4ClassRunner.class)会在测试结束时强制回滚对db的更改。谢谢你的帮助!
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="booksrus">      
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create" /> 
        </properties>
    </persistence-unit>
</persistence>
package bookstore;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import stemen.entity.User;
import stemen.repository.UserRepository;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:simple-repository-context.xml")
@Transactional
public class TestUserRepository {
    private final Logger LOGGER = Logger.getLogger(TestUserRepository.class);

    @Autowired
    UserRepository repository;
    private User tom;
    private User patrick;
    private User john;

    @Before
    public void setUp() {
        tom = new User("123 California", "Apt 143", "LA", "Tom@gmail.com", "Tom", "Hanks", "Itsasecret", "CA","54221");
        patrick = new User("847 Mapple Dr.", "", "Washington", "Patrick@gmail.com", "Patrick", "Steward", "moneyMonkey", "MD","64541");
        john = new User("8484 Bristol", "", "Columbus", "john@gmail.com", "John", "Roberts", "pass", "OH","57963");
        repository.save(tom);
        repository.save(patrick);
        repository.save(john);
        assertThat(repository.count(), equalTo(3L));
    }



    /**
     * Tests inserting a user and asserts it can be loaded again.
     */
    @Test
    public void testThatTomCanBeInserted() {
        User retrievedUser = repository.save(tom);
        assertThat(retrievedUser, equalTo(tom));
        assertEquals(tom, repository.findOne(retrievedUser.getId()));
    }

    @Test
    public void testThatJohnCanBeFoundByEmailAndPassword(){
        User retreivedUser = repository.findUserByEmailIgnoreCaseAndPassword(john.getEmail(), john.getPassword());
        assertThat(retreivedUser, equalTo(john));
    }

}