使用LocalContainerEntityManagerFactoryBean创建会话?

时间:2016-02-24 14:59:30

标签: java spring hibernate session jpa

我第一次使用function SetPortfolioItems(filter) { var str = filter.toLowerCase(); var invalidItems = $( ".portfolio-item:not(."+str+")"); invalidItems.hide("slow"); var validItems = $( ".portfolio-item." + str ); validItems.show("slow"); } SetPortfolioItems('filter');。是否可以使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="portfolio-item">item 1</div> <div class="portfolio-item filter">item 2</div> <div class="portfolio-item">item 3</div> <div class="portfolio-item filter">item 4</div> <div class="portfolio-item">item 5</div> <div class="portfolio-item">item 6</div>来创建类似于hibernate会话的内容?

我熟悉从Spring和JPA中的LocalContainerEntityManagerFactoryBean创建一个休眠会话。

但是如何使用hibernate.cfg.xml才能对我的数据库进行交易呢?

1 个答案:

答案 0 :(得分:1)

您必须定义将配置到LocalContainerEntityManagerFactoryBean的jpa事务管理器,例如:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>



<bean id="entityManagerFactory"

        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="enginePU" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
           <props>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.default_schema">dbo</prop>
                <prop key="hibernate.default_catalog">ab</prop>
           </props>
        </property>
      </bean>

然后,如果您将事务配置为注释驱动:

<tx:annotation-driven />

你可以在你的daos中使用entityManager:

    @PersistenceContext
    protected EntityManager entityManager;


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void persist(Object o) throws IOException {
         entityManager.persist(o);
    }

希望它有所帮助。