我正在尝试在我的junit类中创建实体管理器但是我没有成功,直到请告知我如何克服下面是我的实体管理器配置
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="aaa" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<entry key="hibernate.cache.use_query_cache" value="false" />
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.use_sql_comments" value="false" />
<entry key="hibernate.format_sql" value="false" />
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.ytr.cyt.tc.*" />
以下是我试图创建实体管理器但仍然无法成功完成的junit类,请告知如何克服这个问题,我在junm的xml配置中观察到了另外一件事entityManager未定义
@ContextConfiguration(locations = { "applicationContext.xml" })
@TransactionConfiguration(defaultRollback=true)
@Transactional
public class test4 {
@PersistenceContext
protected static EntityManager em ;
//***** enetity manager is still null ****
@BeforeClass
public static void setUpClass() throws Exception {
if (em == null) {
em = (EntityManager) Persistence.createEntityManagerFactory("aaa").createEntityManager();
System.out.println(em.toString());
//***** enetity manager is still null ****
}
}
答案 0 :(得分:0)
你看过docs for Hibernate了吗?有一个例子,我认为这是你需要的。 此外,还有一些示例可以将entityManeger与实体一起使用,例如this
答案 1 :(得分:0)
可以像下面这样以编程方式初始化EntityManagerFactory:
<f1 somecommand >f2 2>f3
}