当前位置:   article > 正文

jpa multi datasource_jpa mulity datasource

jpa mulity datasource
  1. <persistence-unit name="com.demo" >
  2. <mapping-file>META-INF/orm.xml</mapping-file>
  3. <properties>
  4. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
  5. <property name="hibernate.hbm2ddl.auto" value="validation" />
  6. <property name="hibernate.format_sql" value="true"/>
  7. <property name="hibernate.show_sql" value="false"/>
  8. </properties>
  9. </persistence-unit>

  1. <bean id="emfDemo"
  2. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  3. <property name="dataSource" ref="dataSourceDemo" />
  4. <property name="jpaVendorAdapter">
  5. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  6. <property name="generateDdl" value="true" />
  7. <property name="showSql" value="false" />
  8. </bean>
  9. </property>
  10. <property name="loadTimeWeaver">
  11. <bean
  12. class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
  13. </property>
  14. <property name="persistenceUnitName" value="com.demo" />
  15. </bean>

  1. <filter-mapping>
  2. <filter-name>OpenEntityManagerInViewFilter1</filter-name>
  3. <url-pattern>/*</url-pattern>
  4. </filter-mapping>
  5. <filter>
  6. <filter-name>OpenEntityManagerInViewFilter2</filter-name>
  7. <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
  8. <init-param>
  9. <param-name>entityManagerFactoryBeanName</param-name>
  10. <param-value>emfDemo</param-value>
  11. </init-param>
  12. </filter>

  1. @PersistenceContext(unitName="emfDemo")
  2. public EntityManager entityManager;

  1. @GET
  2. @Path("/jpa/test")
  3. @Transactional
  4. public Response testJpaDemo(@QueryParam ("type") String type){
  5. this.getService().getDao().findAll();
  6. News news = this.getService().getDao().findOne(1L);
  7. News newsMaster = (News) entityManager.find(News.class,2L);
  8. logger.debug(news.getContent());
  9. logger.debug(newsMaster.getContent());
  10. News entity = new News();
  11. entity.setContent("main");
  12. this.getService().getDao().saveAndFlush(entity);
  13. News entityMaster = new News();
  14. entity.setContent("master");
  15. entityManager.persist(entityMaster);
  16. if(true)
  17. throw new GeneralException("", "test transaction");
  18. return Response.ok().build();
  19. }




本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/954987
推荐阅读
相关标签
  

闽ICP备14008679号