app.properties
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/nt
database.username=root
database.password=qwerty
#spring database configuration
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
#Hibernate related properties
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
HibernateConfiguration.java
package com.projectName.mvc.configuration;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.projectName.dao" })
@PropertySource(value = { "classpath:application.properties" })
@PropertySource(value = "file:${CONF_DIR}/application-override.properties", ignoreResourceNotFound = true)
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.projectName.entity" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("database.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("database.url"));
dataSource.setUsername(environment.getRequiredProperty("database.username"));
dataSource.setPassword(environment.getRequiredProperty("database.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("spring.datasource.testOnBorrow",
environment.getRequiredProperty("spring.datasource.testOnBorrow"));
properties.put("spring.datasource.validationQuery",
environment.getRequiredProperty("spring.datasource.validationQuery"));
return properties;
}
@Bean
@Autowired
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
GenericDaoImpl.java
package com.projectName.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.transaction.Transactional;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.projectName.entity.AbstractEntity;
@Repository("genericDao")
@Transactional
public class GenericDaoImpl implements GenericDao {
@Autowired
protected SessionFactory sessionFactory;
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public <E extends AbstractEntity> E create(E model) {
entityManager.persist(model);
return model;
}
@Override
public <E extends AbstractEntity> E update(E model) {
model = entityManager.merge(model);
entityManager.flush();
return model;
}
@Override
public <E extends AbstractEntity> void delete(E model) {
entityManager.remove(model);
}
@Override
public <E extends AbstractEntity> E getById(Class<E> modelClass, Serializable id) {
return entityManager.find(modelClass, id);
}
@Override
public <E extends AbstractEntity> void deleteById(Class<E> modelClass, Serializable id) {
final E entity = getById(modelClass,id);
entityManager.remove(entity);
}
@Override
public <E extends AbstractEntity> List<E> getAll(Class<E> model) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(model);
criteriaQuery.from(model);
return entityManager.createQuery(criteriaQuery).getResultList();
}
}