Hibernate 4.3 SchemaUpdate与JPA [英] Hibernate 4.3 SchemaUpdate with JPA

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

问题描述

我正在尝试编写一个基于Hibernate提供的SchemaUpdate类的Liquibase变更集生成器。
目标是获得Hibernate更新脚本的好处以及开发人员根据需要调整它们的可能性。
完成后,SQL将被集成到一个Liquibase SQL文件中。



开始时看起来非常简单,通过实例化SchemaUpdate类并启动执行方法。



但是代码执行任何操作。我试图从JPA persistence.xml文件初始化Hibernate,我认为这是问题。



目前我已经手动设置了mysql属性,但Hibernate没有找到实体类。
$ b

从JPA配置获得配置(SchemaUpdate类所需)的最佳方式是什么?



我已经搜索了一段时间的解决方案,但是他们都使用了EJB3Configuration类,而这个类在Hibernate 4.3中不再存在。



Thaks for Help !

例如,我正在做这样的事情:

  final File persistenceFile = new File(src / main / resources / META-INF / persistence.xml); 

final配置cfg =新配置(); $!
$ b $ if(!persistenceFile.exists()){

抛出new FileNotFoundException(persistenceFile.getAbsolutePath()+not found);
}

最终列表< String> jpaConfiguration = Files.readLines(persistenceFile,Charsets.UTF_8);

(final String line:jpaConfiguration){

if(line.contains(property)){

final String []道具= line.split(\\\);

cfg.setProperty(道具[1],道具[3]);
}



final SchemaUpdate updater = new SchemaUpdate(cfg);

updater.setOutputFile(target / script.sql);

updater.execute(true,false);


解决方案

有一个缺点:你必须包含'package-info.java',如果有的话。有一个更简单的解决方案(至少现在是这样)。

HibernatePersistenceProvider 可以生成 EntityManagerFactoryBuilder
EntityManagerFactoryBuilderImpl 有一个返回配置的函数。被保护,因此必须派生HibernatePersistenceProvider。

 隐私te class MyHibernatePersistence extends HibernatePersistenceProvider {
$ b $ @Override
public EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName,Map properties){
return super.getEntityManagerFactoryBuilderOrNull(persistenceUnitName,properties);






这可以很容易地创建一个配置,记录所有数据,这些数据也用于EntityManager。

  public配置getConfiguration(String persistenceUnitName,Map< String,Object>属性){
MyHibernatePersistence hpp = new MyHibernatePersistence();
EntityManagerFactoryBuilder emfb = hpp.getEntityManagerFactoryBuilderOrNull(persistenceUnitName,properties));
if(emfb instanceof EntityManagerFactoryBuilderImpl){
emfb.build();
return((EntityManagerFactoryBuilderImpl)emfb).getHibernateConfiguration();
}
返回null;
}


I am trying to write a Liquibase changeset generator based on SchemaUpdate class provided by Hibernate. The goal is to have the benefits of Hibernate update scripts and the possibility for developpers to tweak them as needed. When done, the SQL will be intergated in a Liquibase SQL file.

This seemed very simple at the beginning, by instanciating a SchemaUpdate class and launch the execute method.

But the code does anything. I am trying to initialize Hibernate from a JPA persistence.xml file and I think that's the problem.

For the moment I have manually set mysql properties, but Hibernate doesn't find the entity classes.

What's the best way to get a configuration (needed by the SchemaUpdate class) from JPA configuration ?

I've searched a solution for a while now, but all of them use EJB3Configuration class which doesn't exist anymore in Hibernate 4.3.

Thaks for Help !

For example, I am doing something like this :

final File persistenceFile = new File("src/main/resources/META-INF/persistence.xml");

final Configuration cfg = new Configuration();

if ( ! persistenceFile.exists()) {

     throw new FileNotFoundException(persistenceFile.getAbsolutePath() + " not found");
}

final List<String> jpaConfiguration = Files.readLines(persistenceFile, Charsets.UTF_8);

for (final String line : jpaConfiguration) {

    if (line.contains("property")) {

        final String[] props = line.split("\\\"");

        cfg.setProperty(props[1], props[3]);
    }

}

final SchemaUpdate updater = new SchemaUpdate(cfg);

updater.setOutputFile("target/script.sql");

updater.execute(true, false);

解决方案

This solution has a drawback. You must also include the 'package-info.java', if there is one. There is a simpler solution (at least for now).

HibernatePersistenceProvider can generate EntityManagerFactoryBuilder and EntityManagerFactoryBuilderImpl has a function to give a Configuration back. Unfortunately, this function is protected, so must HibernatePersistenceProvider be derived.

private class MyHibernatePersistence extends HibernatePersistenceProvider {

    @Override
    public EntityManagerFactoryBuilder getEntityManagerFactoryBuilderOrNull(String persistenceUnitName, Map properties) {
        return super.getEntityManagerFactoryBuilderOrNull(persistenceUnitName, properties);
    }
}

This can be easily created a Configuration, which takes into account all the data, which are also used for a EntityManager.

public Configuration getConfiguration(String persistenceUnitName, Map<String, Object> properties) {
    MyHibernatePersistence hpp = new MyHibernatePersistence();
    EntityManagerFactoryBuilder emfb = hpp.getEntityManagerFactoryBuilderOrNull(persistenceUnitName, properties));
    if (emfb instanceof EntityManagerFactoryBuilderImpl) {
        emfb.build();
        return ((EntityManagerFactoryBuilderImpl) emfb).getHibernateConfiguration();
    }
    return null;
}

这篇关于Hibernate 4.3 SchemaUpdate与JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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