EJB3 CMP + Hibernate AnnotationConfiguration的编程配置

时间:2010-06-28 07:45:53

标签: java hibernate jpa ejb-3.0

我使用EJB3容器管理持久性,即通过@PersistenceContext注释注入EntityManager。然后,持久化上下文可以传播到嵌套EJB。交易也由容器(glassfish)管理。

通常我会将persistence.xml放到META-INF目录中,容器会找出使用哪个提供程序以及如何配置EntityManagerFactory(基于hibernate特定属性)。

我的问题是我需要挂钩到EntityManagerFactory配置过程。 特别是我需要在EntityManagerFactory配置之前更改某些PersistentClasses中的鉴别器值(对任何更改都是冻结的)。

这是我使用Spring的方式,但需要与纯EJB3 CMP类似(或者可以在Spring的帮助下)。

public class AnnotationSessionFactoryBean extends  org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean {
    /** Log4j logging instance. */
    protected static Logger log = Logger.getLogger(AnnotationSessionFactoryBean.class);

    //some data preloaded from the database using jdbc
    private Map<String, DatabaseConfiguration> configs;

    @Override
    protected void postProcessAnnotationConfiguration(AnnotationConfiguration config) throws HibernateException {
        //Load and process dynamic Mappings.
        Iterator classMappingsIter = config.getClassMappings();
        while(classMappingsIter.hasNext()) {
            PersistentClass persistentClass = (PersistentClass) classMappingsIter.next();

            String discriminatorValue = persistentClass.getDiscriminatorValue();
            if(discriminatorValue != null) {
                log.debug("DiscriminatorValue before [" + discriminatorValue + "]");
                //here I replace discriminator values.
                //The Discriminator values are coded in the annotations
                //as names (words). These words need to be replaced with ids
                //previously loaded from the database using jdbc.
                //The names are constant in all environments, however the ids are
                //are different.    
                discriminatorValue = StringUtil.replacePlaceholders(discriminatorValue, configs);
                persistentClass.setDiscriminatorValue(discriminatorValue);
                log.debug("DiscriminatorValue after [" + discriminatorValue + "]");
            }


        }
        super.postProcessAnnotationConfiguration(config);
    }

    /**
     * @return the configs
     */
    public Map<String, DatabaseConfiguration> getConfigs() {
        return configs;
    }

    /**
     * @param configs the configs to set
     */
    public void setConfigs(Map<String, DatabaseConfiguration> configs) {
        this.configs = configs;
    }


}

提前致谢, 安东

2 个答案:

答案 0 :(得分:1)

我想我找到了解决方案。 可以覆盖org.hibernate.ejb.HibernatePersistence类。

public class HibernatePersistenceCustom extends org.hibernate.ejb.HibernatePersistence {
    /** Log4j logging instance. */
    protected static Logger log = Logger.getLogger(HibernatePersistenceCustom.class);

    @Override
    public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
        Ejb3Configuration cfg = new Ejb3Configuration();
        //here you can configure it
        doCustomConfiguration(cfg);
        Ejb3Configuration configured = cfg.configure(info, map);
        return configured != null ? configured.buildEntityManagerFactory() : null;
    }

    ...
    //other methods can also be overridden if required.


    public void doCustomConfiguration(Ejb3Configuration config) {
        //Load and process dynamic Mappings.
        Iterator classMappingsIter = config.getClassMappings();
        while(classMappingsIter.hasNext()) {
            PersistentClass persistentClass = (PersistentClass) classMappingsIter.next();

            String discriminatorValue = persistentClass.getDiscriminatorValue();
            if(discriminatorValue != null) {
                log.debug("DiscriminatorValue before [" + discriminatorValue + "]");
                //here I replace discriminator values.
                //The Discriminator values are coded in the annotations
                //as names (words). These words need to be replaced with ids
                //previously loaded from the database using jdbc.
                //The names are constant in all environments, however the ids are
                //are different.    
                discriminatorValue = StringUtil.replacePlaceholders(discriminatorValue, configs);
                persistentClass.setDiscriminatorValue(discriminatorValue);
                log.debug("DiscriminatorValue after [" + discriminatorValue + "]");
            }


        }

    } 
}

然后在persistence.xml而不是org.hibernate.ejb.HibernatePersistence中放入com.mydomain.persistence.HibernatePersistenceCustom

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="mypersistenceunit" transaction-type="JTA">
    <provider>com.mydomain.persistence.HibernatePersistenceCustom</provider>
    <jta-data-source>jdbc/mydatasource</jta-data-source>
    <properties>
      <property name="hibernate.show_sql" value="false"/>
      <property name="hibernate.format_sql" value="false"/>
      <property name="hibernate.use_sql_comments" value="false"/>
      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    </properties>
  </persistence-unit>
</persistence>

尚未测试过,但我认为它会起作用。

由于

答案 1 :(得分:0)

您可以通过提供XML映射文件来覆盖元数据注释(请参阅JPA 1.0规范中的第10章XML描述符)。

当然,这不是动态的(除非您使用例如FreeMarker生成XML映射文件并从数据库中提供值)。