Skip to content

Instantly share code, notes, and snippets.

@otwm
Last active April 13, 2018 16:30
Show Gist options
  • Save otwm/11f038e392c62da51367837229bac594 to your computer and use it in GitHub Desktop.
Save otwm/11f038e392c62da51367837229bac594 to your computer and use it in GitHub Desktop.
entityManager, jpa query factory expose
# jpa config
#spring.datasource.url=
spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
#spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle;DB_CLOSE_DELAY=-1
spring.datasource.platform=h2
spring.datasource.continue-on-error=true
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.initialize=true
# data page config
spring.data.rest.default-page-size=10
spring.data.rest.max-page-size=300
# jpa config
spring.data.jpa.repositories.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.open-in-view=true
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.properties.hibernate.physical_naming_strategy=kr.co.rebel9.NamingStrategy
spring.jpa.hibernate.naming.physical-strategy=kr.co.rebel9.NamingStrategy
# data source config
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=xxxx
#spring.datasource.url=xxxx
spring.datasource.username=xxx
spring.datasource.password=xxxx
spring.h2.console.enabled=false
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
package kr.co.rebel9;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;
/**
* Created by kdo on 16. 9. 20.
*/
@Configuration
@EnableAutoConfiguration
public class DatabaseConfig {
@Value("${spring.jpa.database-platform}")
private String databasePlatform;
@Value("${spring.jpa.properties.hibernate.physical_naming_strategy}")
private String nameStrategy;
@Value("${spring.jpa.generate-ddl}")
private Boolean generateDdl;
@Value("${spring.jpa.hibernate.ddl-auto}")
private String hibernateDdlAuto;
@Value("${spring.jpa.open-in-view}")
private String openInView;
@Value("${spring.jpa.show-sql}")
private Boolean showSql;
@Autowired
private JpaVendorAdapter jpaVendorAdapter;
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource defaultDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(defaultDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan("kr.co.rebel9");
factoryBean.setPersistenceUnitName("defaultPersistence");
factoryBean.setJpaProperties(additionalProperties());
return factoryBean;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", databasePlatform);
properties.setProperty("hibernate.hbm2ddl.auto", hibernateDdlAuto);
properties.setProperty("hibernate.physical_naming_strategy", nameStrategy);
return properties;
}
@Primary
@Bean(name = "transactionManager")
PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory().getObject());
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(showSql);
hibernateJpaVendorAdapter.setDatabasePlatform(databasePlatform);
hibernateJpaVendorAdapter.setGenerateDdl(generateDdl);
hibernateJpaVendorAdapter.setDatabase(Database.H2);
return hibernateJpaVendorAdapter;
}
@Bean
public EntityManager entityManager(){
return entityManagerFactory().getObject().createEntityManager();
}
@Bean
public JPAQueryFactory JPAQueryFactory(){
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager());
return queryFactory;
}
}
package kr.co.rebel9;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import java.util.Locale;
/**
* Created by kdo on 16. 10. 27.
*/
public class NamingStrategy extends PhysicalNamingStrategyStandardImpl implements PhysicalNamingStrategy {
private final String tablePrefix = "tb_";
public static final NamingStrategy INSTANCE = new NamingStrategy();
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(tablePrefix + addUnderscores(name.getText()), name.isQuoted());
}
protected static String addUnderscores(String name) {
final StringBuilder buf = new StringBuilder(name.replace('.', '_'));
for (int i = 1; i < buf.length() - 1; i++) {
if (
Character.isLowerCase(buf.charAt(i - 1)) &&
Character.isUpperCase(buf.charAt(i)) &&
Character.isLowerCase(buf.charAt(i + 1))
) {
buf.insert(i++, '_');
}
}
return buf.toString().toLowerCase(Locale.ROOT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment