View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.logging.log4j.catalog.jpa.config;
17  
18  import org.springframework.beans.factory.annotation.Autowired;
19  import org.springframework.context.annotation.Bean;
20  import org.springframework.context.annotation.Configuration;
21  import org.springframework.context.annotation.Profile;
22  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
23  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
24  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
25  import org.springframework.orm.jpa.JpaTransactionManager;
26  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
27  import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
28  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
29  import org.springframework.transaction.PlatformTransactionManager;
30  import org.springframework.transaction.annotation.EnableTransactionManagement;
31  
32  import javax.persistence.EntityManagerFactory;
33  import javax.sql.DataSource;
34  
35  @Configuration
36  @EnableJpaRepositories("org.apache.logging.log4j.catalog.jpa.dao")
37  @EnableTransactionManagement
38  @Profile("hibernate")
39  public class HibernateConfig {
40  
41      @Autowired
42      private DataSourceConfig dataSourceConfig;
43  
44      @Bean
45      public EntityManagerFactory entityManagerFactory() {
46          AbstractJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
47          vendorAdapter.setGenerateDdl(false);
48  
49          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
50          factory.setJpaVendorAdapter(vendorAdapter);
51          factory.setPackagesToScan("org.apache.logging.log4j.catalog");
52          factory.setDataSource(dataSourceConfig.dataSource());
53          factory.afterPropertiesSet();
54  
55          return factory.getObject();
56      }
57  
58      @Bean
59      public PlatformTransactionManager transactionManager() {
60          JpaTransactionManager txManager = new JpaTransactionManager();
61          txManager.setEntityManagerFactory(entityManagerFactory());
62          return txManager;
63      }
64  }