1. java prject

Getting Started with the project



  1. Go to https://start.spring.io/
  2. Add dependencies:- Web, JPA, MySQL
  3. Download and Extract the project
  4. Import to Eclipse as an existing maven project
  5. Create following packages:
    • model, controller, service, dao, config
  6. Goto src/main/resources and add followings to the application.properties file


# Database
db.driver: com.mysql.jdbc.Driver
db.url: jdbc:mysql://localhost:3306/loginapp
db.username: root
db.password: 64956495

# Hibernate
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql: true
#hibernate.hbm2ddl.auto: create-drop
hibernate.hbm2ddl.auto: validate
#hibernate.hbm2ddl.auto: update
entitymanager.packagesToScan: com

       7. Create DatabaseConfig.java file inside config package

package com.viraj.loginapp.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

@Autowired
private Environment env; // to refer to the application.properties

@Autowired
private DataSource dataSource;

@Autowired
private LocalContainerEntityManagerFactoryBean entityManagerFactory;


/**
* DataSource definition for database connection. Settings are read from
* the application.properties file (using the env object).
*/
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}


/**
* Declare the JPA entity manager factory.
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

entityManagerFactory.setDataSource(dataSource);

// Classpath scanning of @Component, @Service, etc annotated class
entityManagerFactory.setPackagesToScan( env.getProperty("entitymanager.packagesToScan"));

// Vendor adapter
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

// Hibernate properties
Properties additionalProperties = new Properties();
additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
entityManagerFactory.setJpaProperties(additionalProperties);

return entityManagerFactory;
}

/**
* Declare the transaction manager.
*/
@Bean
public JpaTransactionManager transactionManager() {

JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
return transactionManager;
}

/**
* PersistenceExceptionTranslationPostProcessor is a bean post processor
* which adds an advisor to any bean annotated with Repository so that any
* platform-specific exceptions are caught and then rethrown as one
* Spring's unchecked data access exceptions (i.e. a subclass of
* DataAccessException).
*/
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}


}

8. You can start the project now

9. Add following property to your repository file

@PersistenceContext
private EntityManager entityManager;

public EntityManager getEntityManager() {
return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;

}



Comments