如何在JPA和Hibernate 4.3中使用SchemaExportTool [英] How to use SchemaExportTool with JPA and Hibernate 4.3

查看:149
本文介绍了如何在JPA和Hibernate 4.3中使用SchemaExportTool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate 4.3 Ejb3Configuration类被删除。这个类通常用于从持久性单元(persistence.xml文件)创建hibernate配置文件到SchemaExport工具。



作为将模式导出为.sql文件的简单替代方法我使用以下代码:

  public static void export(String persistenceUnit,String exportFileName){
Map<字符串,字符串> hash = new HashMap< String,String>();
hash.put(hibernate.hbm2ddl.auto,create-drop);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(
persistenceUnit,hash);
org.hibernate.jpa.internal.EntityManagerFactoryImpl hibFactory =(org.hibernate.jpa.internal.EntityManagerFactoryImpl)factory;
SessionFactoryImpl hibSessionFactory = hibFactory.getSessionFactory();
SchemaExport schema = ReflectUtils.getPrivateFieldValue(
hibSessionFactory,schemaExport);
schema.setOutputFile(exportFileName);
schema.setFormat(false);
schema.setDelimiter(;);
schema.drop(true,false);
schema.create(true,false);
}

在这段代码中,我基本上使用由HibernateSessionFactoryImpl创建的schemaexport对象。缺点是每次执行时都会重新创建数据库模式。有没有其他简单的方法与Hibernate 4.3和JPA一起使用SchemaExporTool?看来真正的问题是如何从persistenceunit创建Hibernate配置对象?

解决方案

我遇到了同样的问题。我最终通过使用Hibernate的PersistenceXmlParser来访问persistence.xml文件中的信息并手动创建Configuration对象:

  public static void main(String [] args){

PersistenceXmlParser parser = new PersistenceXmlParser(new ClassLoaderServiceImpl(),PersistenceUnitTransactionType.RESOURCE_LOCAL);
列表< ParsedPersistenceXmlDescriptor> allDescriptors = parser.doResolve(new HashMap<>()); (ParsedPersistenceXmlDescriptor描述符:allDescriptors)

{

配置cfg = new Configuration();
cfg.setProperty(hibernate.hbm2ddl.auto,create);
cfg.setProperty(hibernate.dialect,org.hibernate.dialect.Oracle10gDialect);
cfg.setProperty(hibernate.id.new_generator_mappings,true);

列表< String> managedClassNames = descriptor.getManagedClassNames();
for(String className:managedClassNames){
try {
cfg.addAnnotatedClass(Class.forName(className));
} catch(ClassNotFoundException e){
System.out.println(Class not found:+ className);
}
}

SchemaExport export = new SchemaExport(cfg);
export.setDelimiter(;);
export.setOutputFile(C:\\dev\\+ descriptor.getName()+_create_schema.sql);
export.setFormat(true);
export.execute(true,false,false,false);

}
}


At Hibernate 4.3 Ejb3Configuration class was removed. This class was commonly used for creating hibernate configuration file from a persistence unit (persistence.xml file) to SchemaExport tool.

As a simple alternative to export schema to .sql file I'm using the following code:

public static void export(String persistenceUnit, String exportFileName) {
        Map<String, String> hash = new HashMap<String, String>();
        hash.put("hibernate.hbm2ddl.auto", "create-drop");
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(
                persistenceUnit, hash);
        org.hibernate.jpa.internal.EntityManagerFactoryImpl hibFactory = (org.hibernate.jpa.internal.EntityManagerFactoryImpl) factory;
        SessionFactoryImpl hibSessionFactory = hibFactory.getSessionFactory();
        SchemaExport schema = ReflectUtils.getPrivateFieldValue(
                hibSessionFactory, "schemaExport");
        schema.setOutputFile(exportFileName);
        schema.setFormat(false);
        schema.setDelimiter(";");
        schema.drop(true, false);
        schema.create(true, false);
    }

At this piece of code, i'm basically using schemaexport object created by HibernateSessionFactoryImpl. The drawback is that every time it´s executed the database schema is recreated. Is there any other simple way to use SchemaExporTool with Hibernate 4.3 and JPA? It seems that the real question is how to create Hibernate Configuration Object from a persistenceunit?

解决方案

I ran into the same Problem. I ended up by using the internal PersistenceXmlParser of Hibernate to access information in the persistence.xml file and creating the Configuration object manually:

public static void main(String[] args) {

    PersistenceXmlParser parser = new PersistenceXmlParser(new ClassLoaderServiceImpl(), PersistenceUnitTransactionType.RESOURCE_LOCAL);
    List<ParsedPersistenceXmlDescriptor> allDescriptors = parser.doResolve(new HashMap<>());

    for (ParsedPersistenceXmlDescriptor descriptor : allDescriptors) {

        Configuration cfg = new Configuration();
        cfg.setProperty("hibernate.hbm2ddl.auto", "create");
        cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        cfg.setProperty("hibernate.id.new_generator_mappings", "true");

        List<String> managedClassNames = descriptor.getManagedClassNames();
        for (String className : managedClassNames) {
            try {
                cfg.addAnnotatedClass(Class.forName(className));
            } catch (ClassNotFoundException e) {
                System.out.println("Class not found: " + className);
            }
        }

        SchemaExport export = new SchemaExport(cfg);
        export.setDelimiter(";");
        export.setOutputFile("C:\\dev\\" + descriptor.getName() + "_create_schema.sql");
        export.setFormat(true);
        export.execute(true, false, false, false);

    }
}

这篇关于如何在JPA和Hibernate 4.3中使用SchemaExportTool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆