简体   繁体   中英

generating database from hibernate entities

I have mysql db from which I generated the hibernate entities, now I need to generate in-memory database from these entities for testing. I got this error while trying to run my unit test.

/*** main] ohengine.jdbc.spi.SqlExceptionHelper : SQL Error: 42102, SQLState: 42S02 2016-02-16 18:10:47.864 ERROR 29758 --- [ main] ohengine.jdbc.spi.SqlExceptionHelper : Table "tbl_all_orders" not found; SQL statement: **/

It looks like the db creation failed.

Here is my testing properties file content:

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

This is my example for h2 testing. You can alter it a bit and see where it fits in your case. mainly you need to create db tables manually and let your config.xml to have your db included. You can create .sql file manually and create tables and let the bean to include it if you are using spring.

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 file

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 
);

...add your constrains or columns accordingly...

test_data.sql file

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

this is my working configuration for testing (database.properties inside src/test/resources folder)

# 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

btw, your unit tests shouldn't be hitting the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM