gpt4 book ai didi

java - EJB3 CMP + Hibernate AnnotationConfiguration 的编程配置

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:54 26 4
gpt4 key购买 nike

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

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

我的问题是我需要 Hook 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;
}


}

提前致谢,安东

最佳答案

我想我已经找到了解决方案。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>

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

谢谢

关于java - EJB3 CMP + Hibernate AnnotationConfiguration 的编程配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3130775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com