Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.64 KB | None | 0 0
  1. <properties>
  2. <!-- Persistence and Validation-->
  3. <hibernate.version>4.1.0.Final</hibernate.version>
  4. <hibernate.jpa.version>1.0.1.Final</hibernate.jpa.version>
  5. <javax.validation.version>1.0.0.GA</javax.validation.version>
  6. <querydsl.version>2.2.5</querydsl.version>
  7. <spring.jpa.version>1.2.0.RELEASE</spring.jpa.version>
  8. <spring.ldap.version>1.3.1.RELEASE</spring.ldap.version>
  9.  
  10. <!-- Spring and Logging -->
  11. <spring.version>3.1.3.RELEASE</spring.version>
  12. <spring.security.version>3.1.3.RELEASE</spring.security.version>
  13. <slf4j.version>1.6.4</slf4j.version>
  14. <jackson.version>1.9.9</jackson.version>
  15.  
  16. <cglib.version>3.0</cglib.version>
  17. </properties>
  18.  
  19. @Bean
  20. public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { // Important line (notice entityManagerFactory is 'provided/autowired'
  21. return new JpaTransactionManager(entityManagerFactory);
  22. }
  23.  
  24. @Bean
  25. public EntityManagerFactory getEntityManagerFactory(DataSource dataSource) { // Important line (notice dataSource is 'provided/autowired'
  26.  
  27. LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  28. factoryBean.setDataSource(dataSource);
  29. factoryBean.setPackagesToScan("my.scanned.domain");
  30.  
  31. AbstractJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  32. vendorAdapter.setGenerateDdl(true);
  33. vendorAdapter.setShowSql(false);
  34. vendorAdapter.setDatabase(Database.POSTGRESQL);
  35. vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQL82Dialect");
  36.  
  37. factoryBean.setJpaVendorAdapter(vendorAdapter);
  38.  
  39. Map<String, Object> properties = new HashMap<>();
  40. properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
  41. properties.put( "hibernate.bytecode.provider", "cglib" ); // Suppose to help java pergem space issues with hibernate
  42.  
  43. factoryBean.setPersistenceProvider(new HibernatePersistence());
  44. factoryBean.setJpaPropertyMap(properties);
  45. factoryBean.setPersistenceUnitName("myPersistenace");
  46. factoryBean.afterPropertiesSet();
  47.  
  48. return factoryBean.getObject(); // Important line
  49. }
  50.  
  51. @Bean
  52. public PersistenceExceptionTranslator getHibernateExceptionTranslator() { // Required
  53. return new HibernateExceptionTranslator();
  54. }
  55.  
  56. @Bean
  57. public DataSource getDataSource() {
  58. JndiDataSourceLookup lookup = new JndiDataSourceLookup();
  59. DataSource dataSource = lookup.getDataSource("java:comp/env/jdbc/myLookup");
  60.  
  61. lookup = null;
  62.  
  63. return dataSource;
  64. }
  65.  
  66. @Configuration
  67. @EnableTransactionManagement
  68. @ImportResource( "classpath*:*jpa-repository-context.xml" )
  69. @ComponentScan( basePackages = { "data.repository" } )
  70. public class PersistenceJpaConfig
  71. {
  72. @Bean
  73. public LocalContainerEntityManagerFactoryBean entityManagerFactory()
  74. {
  75. LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  76. factoryBean.setDataSource( dataSource() );
  77. factoryBean.setPackagesToScan( new String[] { "data.domain" } );
  78.  
  79. // Setup vendor specific information. This will depend on the chosen DatabaseType
  80. HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  81. vendorAdapter.setGenerateDdl( true );
  82. vendorAdapter.setShowSql( false );
  83. vendorAdapter.setDatabasePlatform( "org.hibernate.dialect.PostgreSQL82Dialect" );
  84.  
  85. factoryBean.setJpaVendorAdapter( vendorAdapter );
  86.  
  87. Map<String, Object> properties = new HashMap<String, Object>();
  88. properties.put( "hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy" );
  89.  
  90. factoryBean.setJpaPropertyMap( properties );
  91.  
  92. return factoryBean;
  93. }
  94.  
  95. @Bean
  96. public DataSource dataSource()
  97. {
  98. JndiDataSourceLookup lookup = new JndiDataSourceLookup();
  99. DataSource dataSource;
  100.  
  101. dataSource = lookup.getDataSource( "java:comp/env/jdbc/postgres" );
  102.  
  103.  
  104. return dataSource;
  105. }
  106.  
  107. @Bean
  108. public PlatformTransactionManager transactionManager()
  109. {
  110. JpaTransactionManager transactionManager = new JpaTransactionManager();
  111. transactionManager.setEntityManagerFactory( entityManagerFactory().getObject() );
  112.  
  113. return transactionManager;
  114. }
  115. }
  116.  
  117. public interface UserAccountRepository extends JpaRepository<UserAccount, Long>, QueryDslPredicateExecutor<UserAccount> {
  118. }
  119.  
  120. @Component
  121. public class UserAccountService {
  122.  
  123. @Autowired
  124. private UserAccountRepository userAccountRepository;
  125.  
  126. public List<UserAccount> getUserAccounts() {
  127. return userAccountRepository.findAll();
  128. }
  129. ...
  130. }
  131.  
  132. <properties>
  133. <!-- Persistence and Validation-->
  134. <hibernate.version>4.1.0.Final</hibernate.version>
  135. <hibernate.jpa.version>1.0.1.Final</hibernate.jpa.version>
  136. <javax.validation.version>1.0.0.GA</javax.validation.version>
  137. <querydsl.version>2.2.5</querydsl.version>
  138. <spring.jpa.version>1.1.0.RELEASE</spring.jpa.version>
  139.  
  140. <!-- Spring and Logging -->
  141. <spring.version>3.1.2.RELEASE</spring.version>
  142. <spring.security.version>3.1.2.RELEASE</spring.security.version>
  143. <slf4j.version>1.6.4</slf4j.version>
  144. <jackson.version>1.9.9</jackson.version>
  145.  
  146. <!-- Testing Suites -->
  147. <selenium.version>2.24.1</selenium.version>
  148. </properties>
  149.  
  150. private static final ThreadLocal<Map<Object, Object>> resources =
  151. new NamedThreadLocal<Map<Object, Object>>("Transactional resources");
  152.  
  153. private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations =
  154. new NamedThreadLocal<Set<TransactionSynchronization>>("Transaction synchronizations");
  155.  
  156. private static final ThreadLocal<String> currentTransactionName =
  157. new NamedThreadLocal<String>("Current transaction name");
  158.  
  159. private static final ThreadLocal<Boolean> currentTransactionReadOnly =
  160. new NamedThreadLocal<Boolean>("Current transaction read-only status");
  161.  
  162. private static final ThreadLocal<Integer> currentTransactionIsolationLevel =
  163. new NamedThreadLocal<Integer>("Current transaction isolation level");
  164.  
  165. private static final ThreadLocal<Boolean> actualTransactionActive =
  166. new NamedThreadLocal<Boolean>("Actual transaction active");
  167.  
  168. TransactionSynchronizationManager.clear();
  169.  
  170. Map<Object, Object> ojb = TransactionSynchronizationManager.getResourceMap();
  171. for (Object key : ojb.keySet()) {
  172. TransactionSynchronizationManager.unbindResource(key);
  173. }
  174.  
  175. compile ('org.springframework.boot:spring-boot-starter-thymeleaf:' + bootVersion + '.RELEASE') {
  176. exclude module: 'spring-boot-starter-tomcat'
  177. exclude module: 'hibernate-validator'
  178. }
  179. // https://mvnrepository.com/artifact/org.hibernate/hibernate-validator
  180. compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.2.4.Final'
  181.  
  182. group 'com.test'
  183. version '1.0-SNAPSHOT'
  184.  
  185. apply plugin: 'java'
  186. apply plugin: 'war'
  187. sourceCompatibility = 1.8
  188. repositories {
  189. mavenCentral()
  190. }
  191. dependencies {
  192. compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.10.Final'
  193. compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.4.RELEASE'
  194. compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.9.RELEASE'
  195. providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
  196. providedCompile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
  197. compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
  198. }
  199.  
  200. @Configuration
  201. @Import({JPAConfiguration.class})
  202. @EnableWebMvc
  203. public class ApplicationConfig {}
  204.  
  205. @Configuration
  206. @EnableJpaRepositories("com.test.dao")
  207. @EnableTransactionManagement
  208. public class JPAConfiguration {
  209.  
  210. @Bean
  211. public EntityManagerFactory entityManagerFactory() {
  212. LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  213. factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  214. factory.setPackagesToScan("com.test.model");
  215. factory.setDataSource(restDataSource());
  216. factory.setJpaPropertyMap(getPropertyMap());
  217. factory.afterPropertiesSet();
  218. return factory.getObject();
  219. }
  220.  
  221. @Bean(destroyMethod = "close")
  222. public DataSource restDataSource() {
  223. BasicDataSource dataSource = new BasicDataSource();
  224. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  225. dataSource.setUrl("jdbc:mysql://localhost:3306/test");
  226. dataSource.setUsername("test");
  227. dataSource.setPassword("test");
  228. return dataSource;
  229. }
  230.  
  231. private Map<String, String> getPropertyMap() {
  232. Map<String, String> hibernateProperties = new HashMap<>();
  233. hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
  234. hibernateProperties.put("hibernate.show_sql", "true");
  235. hibernateProperties.put("hibernate.format_sql", "true");
  236. hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
  237. return hibernateProperties;
  238. }
  239.  
  240. @Bean
  241. public PlatformTransactionManager transactionManager() {
  242. JpaTransactionManager txManager = new JpaTransactionManager();
  243. txManager.setEntityManagerFactory(entityManagerFactory());
  244. return txManager;
  245. }
  246.  
  247. }
  248.  
  249. @Repository
  250. public interface TestRepository extends JpaRepository<TestEntity, Long> {}
  251.  
  252. @Entity
  253. @Table(name = "ent")
  254. public class TestEntity {
  255. @Id
  256. @GeneratedValue(strategy = GenerationType.IDENTITY)
  257. private Long id;
  258. private String descript;
  259. //equals, hashcode, toString, getters, setters
  260. }
  261.  
  262. public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  263. private WebApplicationContext rootContext;
  264.  
  265. @Override
  266. protected Class<?>[] getRootConfigClasses() {
  267. return new Class[]{ApplicationConfig.class};
  268. }
  269.  
  270. @Override
  271. protected Class<?>[] getServletConfigClasses() {
  272. return null;
  273. }
  274.  
  275. @Override
  276. protected String[] getServletMappings() {
  277. return new String[]{"/"};
  278. }
  279.  
  280. @Override
  281. protected WebApplicationContext createRootApplicationContext() {
  282. rootContext = super.createRootApplicationContext();
  283. return rootContext;
  284. }
  285.  
  286. @Override
  287. public void onStartup(ServletContext servletContext) throws ServletException {
  288. super.onStartup(servletContext);
  289. AnnotationConfigWebApplicationContext restApiContext = new AnnotationConfigWebApplicationContext();
  290. restApiContext.setParent(rootContext);
  291. }
  292. }
  293.  
  294. jmap -histo <tomcat_pid>
  295.  
  296. com.test.config.dao.JPAConfiguration$$EnhancerBySpringCGLIB$$792cb231$$FastClassBySpringCGLIB$$45ff499c
  297. com.test.config.dao.JPAConfiguration$$FastClassBySpringCGLIB$$10104c1e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement