Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ManagedBean
- @ViewScoped
- public class LoginBean implements Serializable
- {
- private String username;
- private String password;
- @Autowired
- private AccountService accountService;
- public LoginBean()
- {
- }
- public void saveAcc()
- {
- System.out.println("Save Acc");
- System.out.println(accountService == null ? "null":"not null");
- //accountService.save(new Account("username", "password", 1));
- }
- //getter and setter omitted
- }
- @Transactional
- @Service
- public class AccountService
- {
- @Autowired
- private AccountRepository accountRepository;
- public AccountRepository getRepository()
- {
- return accountRepository;
- }
- public void save(Account entity)
- {
- accountRepository.save(entity);
- }
- }
- @Repository
- public interface AccountRepository extends CrudRepository<Account, Integer>
- {
- List<Account> findByName(String name);
- }
- @Entity(name = "Account")
- @Table(name = "account")
- public class Account implements Serializable
- {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- @javax.persistence.Id
- private Integer id;
- @Column(name = "username", nullable = false, length = 45)
- private String username;
- @Column(name = "password", nullable = false, length = 45)
- private String password;
- @Column(name = "profileId", nullable = false)
- private int profileId;
- public Account()
- {
- }
- public Account(String username, String password, int profileId)
- {
- this.username = username;
- this.password = password;
- this.profileId = profileId;
- }
- //getter and setter omitted
- }
- #Database Configuration
- db.driver=com.mysql.jdbc.Driver
- db.url=jdbc:mysql://localhost:3306/uass?zeroDateTimeBehavior=convertToNull
- db.username=root
- db.password=password
- #Hibernate Configuration
- hibernate.dialect=org.hibernate.dialect.MySQLDialect
- hibernate.hbm2ddl.auto=create-drop
- hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
- hibernate.show_sql=true
- hibernate.format_sql=true
- @Configuration
- @EnableJpaRepositories(basePackages =
- {
- "com.peter.uass"
- })
- @EnableTransactionManagement
- public class PersistenceContext
- {
- @Bean(destroyMethod = "close")
- DataSource dataSource(Environment env)
- {
- HikariConfig dataSourceConfig = new HikariConfig();
- dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
- dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
- dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
- dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
- return new HikariDataSource(dataSourceConfig);
- }
- @Bean
- LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env)
- {
- LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
- entityManagerFactoryBean.setDataSource(dataSource);
- entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
- entityManagerFactoryBean.setPackagesToScan("com.peter.uass");
- Properties jpaProperties = new Properties();
- jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
- jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
- jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
- jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
- jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
- entityManagerFactoryBean.setJpaProperties(jpaProperties);
- return entityManagerFactoryBean;
- }
- @Bean
- JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
- {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactory);
- return transactionManager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement