Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created October 11, 2019 19:52
Show Gist options
  • Save darbyluv2code/e0af74553524dc0f4ce09756b7969550 to your computer and use it in GitHub Desktop.
Save darbyluv2code/e0af74553524dc0f4ce09756b7969550 to your computer and use it in GitHub Desktop.
Spring @EnableTransactionManagement
package spring.demo.rest.config;
import java.beans.PropertyVetoException;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
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.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
@EnableWebMvc
@ComponentScan("spring.demo.rest")
@PropertySource({"classpath:persistence-mysql.properties"})
@EnableTransactionManagement
public class ApplicationConfig {
//sluzy do pobierania danych z pliku hibernate.properties
@Autowired
private Environment env;
//ustawianie datasource
@Bean
public DataSource myDataSource() {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
dataSource.setUser(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.password"));
dataSource.setInitialPoolSize(convertStringToInt(env.getProperty("connection.pool.initialPoolSize")));
dataSource.setMinPoolSize(convertStringToInt(env.getProperty("connection.pool.minPoolSize")));
dataSource.setMaxPoolSize(convertStringToInt(env.getProperty("connection.pool.maxPoolSize")));
dataSource.setMaxIdleTime(convertStringToInt(env.getProperty("connection.pool.maxIdleTime")));
return dataSource;
}
public int convertStringToInt(String s) {
return Integer.parseInt(s);
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(myDataSource());
sessionFactory.setPackagesToScan(env.getProperty("hibernate.packagesToScan"));
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
public Properties hibernateProperties() {
Properties props = new Properties();
try {
props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
props.setProperty("hibernate.hbm2ddl.auto",env.getProperty("hibernate.hbm2ddl.auto"));
props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
}catch (Exception e) {
e.printStackTrace();
}
return props;
}
@Autowired
@Bean
public HibernateTransactionManager txManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
}
package spring.demo.rest.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import spring.demo.rest.entity.Countries;
@Repository
public class CountriesDaoImpl implements CountriesDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Countries> getCountries() {
Session session = sessionFactory.getCurrentSession();
Query<Countries> countriesQuery = session.createQuery("from Countries",Countries.class);
List<Countries> countriesList = countriesQuery.list();
return countriesList;
}
@Override
public Countries getCountryByName(String countryName) {
Session session = sessionFactory.getCurrentSession();
Countries country = (Countries) session.createCriteria(Countries.class).add(Restrictions.like("country", countryName)).uniqueResult();
return country;
}
@Override
public void saveCountry(Countries country) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(country);
}
@Override
public void deleteCountry(int countryId) {
Session session = sessionFactory.getCurrentSession();
Countries c = session.get(Countries.class, countryId);
session.delete(c);
}
@Override
public Countries getCountryById(int countryId) {
Session session = sessionFactory.getCurrentSession();
Countries country = session.get(Countries.class, countryId);
return country;
}
@Override
public void updateCountry(Countries country) {
Session session = sessionFactory.getCurrentSession();
session.update(country);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment