使用Hibernate Ejb3Configuration与容器管理 [英] Using Hibernate Ejb3Configuration with Container Management

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

问题描述

有没有办法使用Ejb3Configuration提供的程序配置与容器管理实体管理?我想动态添加(附加)注释类,这些类是在处理persistence.xml之后通过ServiceLoader发现的。



我尝试扩展HibernatePersistence来覆盖createContainerEntityManagerFactory方法,其中我添加了添加类的功能,但是我发现了两个问题:
1)当我尝试调用EjbConfiguration实例在新的PersistenceProvider中配置(PersistenceUnitInfo,Map)方法,我返回null。
2)在应用程序关闭时,我得到了一个由应用程序服务器(GF 3.0.1和GF 3.1)引发的NPE,源自org.glassfish.persistsnce.jpa.JPAApplicationContainer.closeAllEMFs或org.glassfish.persistence.jpa .JPADeployer.closeEMFs分别



以下是我的PersistenceProvider:

  // left out createEntityManagerFactory for shortvity 

public class DynamicEntityProvider extends HibernatePersistence {

@Override
public EntityManagerFactory createContainerEntityManagerFactory
(PersistenceUnitInfo info,Map props) {

final Ejb3Configuration config = new Ejb3Configuration();
final Ejb3Configuration configured = config.configure(info,props);

if(configured!= null){
//永远不会在这里,配置始终返回null
addDynamicEntities(已配置);
return configured.buildEntityManagerFactory();
}
//记录代码,通知我配置为null
return null;
}

// addDynamicEntities()方法impl

I只需将我的persistence.xml文件中的标签替换为该类,并确实按预期方式调用...只要上面列出的问题=)



任何想法非常感谢!

解决方案

我不知道你是否已经解决了你的问题,但...



它返回null,因为您正在扩展的HibernatePersistence类检查persistence.xml是否将提供程序设置为不是子类,因此它不会将您的类(DynamicEntityProvider)识别为提供者,因此它不解析您的xml。

  if(metadata.getProvider()== null || IMPLEMENTATION_NAME.equalsIgnoreCase (metadata.getProvider())){

IMPLEMENTATION_NAME在哪里

  private static final String IMPLEMENTATION_NAME = HibernatePersistence.class.getName() ; 

您可以通过反思在内部进行更改,但这不是一个好的方式。 / p>

Is there a way to use programmatic configuration provided by Ejb3Configuration with container managed entity management? I would like to dynamically add (additional) annotated classes that were discovered via ServiceLoader after the persistence.xml is processed.

I attempted to extend HibernatePersistence override the createContainerEntityManagerFactory method where I added the functionality to add the classes but I found two problems: 1) When I attempt to call the EjbConfiguration instances configure(PersistenceUnitInfo, Map) method within the new PersistenceProvider, I get null returned. 2) I get a NPE thrown by my app server (both GF 3.0.1 and GF 3.1) at application shutdown originating from either org.glassfish.persistsnce.jpa.JPAApplicationContainer.closeAllEMFs or org.glassfish.persistence.jpa.JPADeployer.closeEMFs, respectively

The following is my PersistenceProvider:

//left out createEntityManagerFactory for brevity

public class DynamicEntityProvider extends HibernatePersistence {

    @Override
    public EntityManagerFactory createContainerEntityManagerFactory 
        (PersistenceUnitInfo info, Map props) {

        final Ejb3Configuration config = new Ejb3Configuration();
        final Ejb3Configuration configured = config.configure(info, props);

        if (configured != null) {
            //never gets here, configured is always returned null
            addDynamicEntities(configured);
            return configured.buildEntityManagerFactory();
        }
        //logging code which notifies me that configured was null
        return null;
    }

    //addDynamicEntities() method impl

I simply replace the tag in my persistence.xml file to the class and it does indeed get called as expected...just with the problems listed above =)

Any ideas would be greatly appreciated!

解决方案

I don't know if you already have solved your issue, but...

It returns null because the HibernatePersistence class that you are extending checks if the persistence.xml has the provider set to it not a subclass, so it doesn't recognize your class(DynamicEntityProvider) as the provider so it doesn't parse your xml.

if ( metadata.getProvider() == null || IMPLEMENTATION_NAME.equalsIgnoreCase(metadata.getProvider()) ) {

Where IMPLEMENTATION_NAME is

private static final String IMPLEMENTATION_NAME = HibernatePersistence.class.getName();

You can change it internally via reflection but it is not a "good" way of doing that.

这篇关于使用Hibernate Ejb3Configuration与容器管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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