Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. @ManagedBean
  2. @ViewScoped
  3. public class LoginBean implements Serializable
  4. {
  5. private String username;
  6. private String password;
  7.  
  8. @Autowired
  9. private AccountService accountService;
  10.  
  11. public LoginBean()
  12. {
  13. }
  14.  
  15. public void saveAcc()
  16. {
  17. System.out.println("Save Acc");
  18. System.out.println(accountService == null ? "null":"not null");
  19. //accountService.save(new Account("username", "password", 1));
  20. }
  21. //getter and setter omitted
  22. }
  23.  
  24. @Transactional
  25. @Service
  26. public class AccountService
  27. {
  28. @Autowired
  29. private AccountRepository accountRepository;
  30.  
  31. public AccountRepository getRepository()
  32. {
  33. return accountRepository;
  34. }
  35.  
  36. public void save(Account entity)
  37. {
  38. accountRepository.save(entity);
  39. }
  40. }
  41.  
  42. @Repository
  43. public interface AccountRepository extends CrudRepository<Account, Integer>
  44. {
  45. List<Account> findByName(String name);
  46. }
  47.  
  48. @Entity(name = "Account")
  49. @Table(name = "account")
  50. public class Account implements Serializable
  51. {
  52. @Id
  53. @GeneratedValue(strategy = GenerationType.AUTO)
  54. @javax.persistence.Id
  55. private Integer id;
  56.  
  57. @Column(name = "username", nullable = false, length = 45)
  58. private String username;
  59.  
  60. @Column(name = "password", nullable = false, length = 45)
  61. private String password;
  62.  
  63. @Column(name = "profileId", nullable = false)
  64. private int profileId;
  65.  
  66. public Account()
  67. {
  68. }
  69.  
  70. public Account(String username, String password, int profileId)
  71. {
  72. this.username = username;
  73. this.password = password;
  74. this.profileId = profileId;
  75. }
  76. //getter and setter omitted
  77. }
  78.  
  79. #Database Configuration
  80. db.driver=com.mysql.jdbc.Driver
  81. db.url=jdbc:mysql://localhost:3306/uass?zeroDateTimeBehavior=convertToNull
  82. db.username=root
  83. db.password=password
  84.  
  85. #Hibernate Configuration
  86. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  87. hibernate.hbm2ddl.auto=create-drop
  88. hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
  89. hibernate.show_sql=true
  90. hibernate.format_sql=true
  91.  
  92. @Configuration
  93. @EnableJpaRepositories(basePackages =
  94. {
  95. "com.peter.uass"
  96. })
  97. @EnableTransactionManagement
  98. public class PersistenceContext
  99. {
  100. @Bean(destroyMethod = "close")
  101. DataSource dataSource(Environment env)
  102. {
  103. HikariConfig dataSourceConfig = new HikariConfig();
  104. dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
  105. dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
  106. dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
  107. dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
  108.  
  109. return new HikariDataSource(dataSourceConfig);
  110. }
  111.  
  112. @Bean
  113. LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env)
  114. {
  115. LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  116. entityManagerFactoryBean.setDataSource(dataSource);
  117. entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  118. entityManagerFactoryBean.setPackagesToScan("com.peter.uass");
  119.  
  120. Properties jpaProperties = new Properties();
  121.  
  122. jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
  123.  
  124. jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
  125.  
  126. jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
  127.  
  128. jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
  129.  
  130. jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
  131.  
  132. entityManagerFactoryBean.setJpaProperties(jpaProperties);
  133.  
  134. return entityManagerFactoryBean;
  135. }
  136.  
  137. @Bean
  138. JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
  139. {
  140. JpaTransactionManager transactionManager = new JpaTransactionManager();
  141. transactionManager.setEntityManagerFactory(entityManagerFactory);
  142. return transactionManager;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement