almira.sample.dao.ExportDatabaseSchemaTest.java Source code

Java tutorial

Introduction

Here is the source code for almira.sample.dao.ExportDatabaseSchemaTest.java

Source

/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package almira.sample.dao;

import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.spi.PersistenceUnitInfo;

import org.hibernate.cfg.Configuration;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Inspired from:
 * http://stackoverflow.com/questions/3393092/schema-export-with-hibernate-annotations
 */
@SuppressWarnings("deprecation") // Alternative: http://seamframework.org/Community/SchemaExport
@ContextConfiguration(locations = { "classpath:/testApplicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class ExportDatabaseSchemaTest {

    @Resource(name = "&entityManagerFactory")
    private LocalContainerEntityManagerFactoryBean entityManagerFactory;

    @Test
    public void exportDatabaseSchema() {
        PersistenceUnitInfo persistenceUnitInfo = entityManagerFactory.getPersistenceUnitInfo();
        Map<String, Object> jpaPropertyMap = entityManagerFactory.getJpaPropertyMap();

        Configuration configuration = new Ejb3Configuration().configure(persistenceUnitInfo, jpaPropertyMap)
                .getHibernateConfiguration();

        SchemaExport schema = new SchemaExport(configuration);
        schema.setOutputFile("../conf/database/db-001-schema.sql");

        // BUG:
        // https://issues.jboss.org/browse/JBIDE-10558?page=com.atlassian.jira.plugin.system.issuetabpanels:changehistory-tabpanel
        schema.create(true, false);
    }
}