本文介绍了如何将Hibernate SchemaUpdate类与JPA persistence.xml一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $

  public static void main(String [] args)throws IOException {
//首先我们准备配置
属性hibProps = new Properties();
hibProps.load(Thread.currentThread()。getContextClassLoader()。getResourceAsStream(jbbconfigs.properties));
配置cfg = new AnnotationConfiguration();
cfg.configure(/ hibernate.cfg.xml)。addProperties(hibProps);

//我们创建了SchemaUpdate,感谢配置
SchemaUpdate schemaUpdate = new SchemaUpdate(cfg);


//更新仅以脚本模式执行
schemaUpdate.execute(true,false);
...

我想在JPA项目中重用此代码,没有hibernate.cfg.xml文件(也没有.properties文件),而是一个persistence.xml文件(在JPA规范中指定的META-INF目录中自动检测)。



我试过这个简单的改编,

  Configuration cfg = new AnnotationConfiguration(); 
cfg.configure();

但是因为这个例外而失败。

<$

$ b
$ b

有人做过吗?
Thanks。

解决方案

Kariem在正确的轨道上,但让我试图澄清。 b
$ b

假设您拥有一个纯粹的JPA标准配置,除了类路径中的Hibernate jar外,没有任何Hibernate特定的配置。如果你在J2SE引导模式下运行,你已经有了一些类似这样的代码,无论是在Java中还是作为Spring配置等等:

  Map< String,Object> props = getJPAProperties(); 
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(persistence-unit-name,props);

要运行SchemaUpdate,只需使用它:

 地图< String,Object> props = getJPAProperties(); 
Ejb3Configuration conf =
new Ejb3Configuration().configure(persistence-unit-name,props);
新的SchemaUpdate(conf.getHibernateConfiguration())。execute(true,false);

我不确定这是如何在容器环境中运行的,但是在简单的J2SE或Spring类型的配置,这就是它的全部。


I've a main method using SchemaUpdate to display at the console what tables to alter/create and it works fine in my Hibernate project:

 public static void main(String[] args) throws IOException {
  //first we prepare the configuration
  Properties hibProps = new Properties();
  hibProps.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jbbconfigs.properties"));
  Configuration cfg = new AnnotationConfiguration();
  cfg.configure("/hibernate.cfg.xml").addProperties(hibProps);

  //We create the SchemaUpdate thanks to the configs
  SchemaUpdate schemaUpdate = new SchemaUpdate(cfg);


  //The update is executed in script mode only
  schemaUpdate.execute(true, false);
  ...

I'd like to reuse this code in a JPA project, having no hibernate.cfg.xml file (and no .properties file), but a persistence.xml file (autodetected in the META-INF directory as specified by the JPA spec).

I tried this too simple adaptation,

Configuration cfg = new AnnotationConfiguration();
cfg.configure();

but it failed with that exception.

Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found

Has anybody done that?Thanks.

解决方案

Kariem is on the right track, but let me attempt to clarify.

Let's assume that you have a vanilla JPA-standard configuration with nothing Hibernate-specific, except for the Hibernate jars on the classpath. If you are running in J2SE bootstrap mode, you already have some code that looks something like this, either in Java or as Spring configuration, etc.:

Map<String, Object> props = getJPAProperties();
EntityManagerFactory emf =
    Persistence.createEntityManagerFactory("persistence-unit-name", props);

To run SchemaUpdate, simply use this instead:

Map<String, Object> props = getJPAProperties();
Ejb3Configuration conf =
    new Ejb3Configuration().configure("persistence-unit-name", props);
new SchemaUpdate(conf.getHibernateConfiguration()).execute(true, false);

I'm not sure how this would operate in a container environment, but in a simple J2SE or Spring type of configuration, that's all there is to it.

这篇关于如何将Hibernate SchemaUpdate类与JPA persistence.xml一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:32