簡體   English   中英

從休眠實體生成數據庫

[英]generating database from hibernate entities

我有從其生成休眠實體的mysql db,現在需要從這些實體生成內存數據庫以進行測試。 嘗試運行單元測試時出現此錯誤。

/ *** main] ohengine.jdbc.spi.SqlExceptionHelper:SQL錯誤:42102,SQLState:42S02 2016-02-16 18:10:47.864錯誤29758 --- [main] ohengine.jdbc.spi.SqlExceptionHelper:“表” tbl_all_orders”未找到; SQL語句:** /

看來數據庫創建失敗。

這是我的測試屬性文件內容:

db.driver: org.h2.Driver
db.url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false
db.username: sa
db.password: 

hibernate.dialect: org.hibernate.dialect.H2Dialect
hibernate.show_sql: true
hibernate.format_sql: true
hibernate.hbm2ddl.auto: create
hibernate.archive.autodetection=class, hbm
entitymanager.packagesToScan: linda

這是我的H2測試示例。 您可以對其進行一些更改,然后查看它適合您的情況。 通常,您需要手動創建數據庫表,並讓config.xml包含您的數據庫。 您可以手動創建.sql文件並創建表,如果使用spring,則讓Bean包含它。

someTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("testConfig.xml") // <-- this xml you need to include
public class PortDaoImplTest {

    private static final Logger log = Logger.getLogger(sysInfoDaoImplTest.class);

    @Autowired
    private sysInfoDaoImpl sysDao;

    @After
    public void tearDown(){
        portDao = null;
    }

    @Test
    public void testGetPort() {
        log.info("Testing getInfo(String id)...");
        SysInfo p = sysDao.getInfo("nysdin2039");
        assertNotNull(p);
    }

testConfig.xml

...xml header...                    

            <!--    h2 driver -->
        <bean id="test.dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="false" >
            <property name="driverClassName" value="org.h2.Driver" />
            <property name="url" value="jdbc:h2:mem:test_mem;DB_CLOSE_DELAY=-1;MODE=ORACLE" />
        </bean>

        <!--    datasource file -->
        <jdbc:initialize-database data-source="test.dataSource">
            <!--        table list -->
            <jdbc:script location="com/yourPath/h2/schema.sql" />
            <jdbc:script location="com/yourPath/h2/test_data.sql" />

        </jdbc:initialize-database>

        <!--    bean def -->
        <bean id="sysInfoDao" class="com.mycompanyName.sysInfoDaoImpl" >
            <property name="log" ref="test.log" />
            <property name="dataSource" ref="test.dataSource" />
        </bean>

....

h2 schema.sql文件

drop table IF EXISTS tbl_all_orders;
CREATE TABLE tbl_all_orders
(
  your_stuff_ID     NUMBER               NOT NULL,
  your_other_column_stuff                VARCHAR2(15)    DEFAULT 
);

...相應地添加約束或列...

test_data.sql文件

INSERT into tbl_all_orders
   (your_column_names... , your_other_column_names...)
VALUES
   (1, 'values',...);

這是我用於測試的工作配置(src / test / resources文件夾中的database.properties)

# DB properties
db.driver=org.h2.Driver
db.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
db.username=sa
db.password=

# Hibernate Configuration
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true

# validate: validate the schema, makes no changes to the database.
# update: update the schema.
# create: creates the schema, destroying previous data.
# create-drop: drop the schema at the end of the session.
hibernate.hbm2ddl.auto=create
entitymanager.packages.to.scan=abcde

順便說一句,您的單元測試不應訪問數據庫。

暫無
暫無

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

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