Hibernate删除数据库表内容

时间:2018-12-28 23:44:21

标签: java hibernate

我尝试使用带有Hibernate的JPA在Java中实现CRUD应用程序。 我有两个独立的测试,分别作用于两个不同的数据库表。当我运行这些测试之一时,将正确填充相应的表,但是当我运行其他测试时,将清空先前填充的表,并填充另一个表。

这是我用于休眠配置的configuration.cfg.xml文件:

<hibernate-configuration>
    <session-factory>

        <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
        <property name="hibernate.connection.url">
            jdbc:sqlite:/Users/th3g3ntl3man/Repository/assignment3/src/main/resources/database.db
        </property>

        <property name="dialect">com.enigmabridge.hibernate.dialect.SQLiteDialect</property>
        <property name="show_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.current_session_context_class">
            org.hibernate.context.internal.ThreadLocalSessionContext
        </property>

        <mapping class="entity.User"/>
        <mapping class="entity.Topic"/>
        <mapping class="entity.Image"/>

    </session-factory>
</hibernate-configuration>

我认为问题出在属性hbm2ddl.auto中,因此我尝试将值从create更改为update,但是当我编译一个测试时,我可以正常运行,但是在运行时第二次测试使用此堆栈跟踪进行测试崩溃:

java.lang.ExceptionInInitializerError
at util.Operation.getByPrimaryKey(Operation.java:34)
at dao.TopicDAO.create(TopicDAO.java:22)
at entity.TopicTest.setUp(TopicTest.java:22)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.util.NoSuchElementException
    at java.base/java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
    at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.populateTablesWithColumns(InformationExtractorJdbcDatabaseMetaDataImpl.java:369)
    at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.getTables(InformationExtractorJdbcDatabaseMetaDataImpl.java:337)
    at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.getTablesInformation(DatabaseInformationImpl.java:120)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:65)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at util.HibernateUtil.init(HibernateUtil.java:11)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:8)

对于所有代码,这是存储库link

0 个答案:

没有答案