Мое требование:
Мое требование - прочитать имя таблицы из файла свойств.
все работает нормально, как и ожидалось, без пользовательской конфигурации базы данных.
когда я объявляю следующий способ, он работает нормально.
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.default_schema=upload
#spring.jpa.hibernate.ddl-auto=create-drop
#sample.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.propertie.hibernate.physical_naming_strategy=com.example.demo.entities.TableNameStrategy.
, но когда я делаю пользовательскую конфигурацию базы данных, я получаю следующую ошибку
Caused by: java.lang.NullPointerException: null
at com.example.demo.entities.TableNameStrategy.toPhysicalTableName(TableNameStrategy.java:25) ~[classes/:na]
at org.hibernate.boot.model.relational.Namespace.createTable(Namespace.java:92) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addTable(InFlightMetadataCollectorImpl.java:772) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.cfg.annotations.TableBinder.buildAndFillTable(TableBinder.java:509) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.cfg.annotations.EntityBinder.bindTable(EntityBinder.java:848) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:647) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
... 17 common frames omitted
Вотмой код конфигурации базы данных :
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="sampleEntityManager",
transactionManagerRef="sampleTransactionManager",
basePackages= {"com.example.demo.repo"})
public class SampleConfiguration {
@Autowired
Environment env;
@Bean(name="sampleDataSource")
@Primary
public DataSource dmsDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("sample.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("sample.datasource.url"));
dataSource.setUsername(env.getProperty("sample.datasource.username"));
dataSource.setPassword(env.getProperty("sample.datasource.password"));
return dataSource;
}
@Primary
@Bean(name = "sampleEntityManager")
public LocalContainerEntityManagerFactoryBean dmsEntityManagerFactory(EntityManagerFactoryBuilder builder) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.jdbc.lob.non_contextual_creation",env.getProperty("sample.jpa.properties.hibernate.jdbc.lob.non_contextual_creation"));
properties.put("hibernate.default_schema", env.getProperty("sample.jpa.properties.hibernate.default_schema"));
properties.put("hibernate.ddl-auto", env.getProperty("sample.jpa.properties.hibernate.ddl-auto"));
properties.put("hibernate.physical_naming_strategy",env.getProperty("sample.jpa.properties.hibernate.physical_naming_strategy"));
return builder
.dataSource(dmsDataSource())
.properties(properties)
.packages("com.example.demo.entities")
.persistenceUnit("dms")
.build();
}
@Primary
@Bean(name = "sampleTransactionManager")
public PlatformTransactionManager dmsTransactionManager(@Qualifier("sampleEntityManager") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
это класс, в котором я определяю имя своей таблицы:
@Configuration
public class TableNameStrategy extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {
public static final PhysicalNamingStrategyStandardImpl INSTANCE = new PhysicalNamingStrategyStandardImpl();
@Autowired
Environment env;
@Override public Identifier toPhysicalTableName(Identifier name,JdbcEnvironment context) {
return new Identifier(name.toIdentifier(env.getProperty("tableName")).getText(),
name.isQuoted());
}
}
Файл моих свойств:
sample.datasource.url=jdbc:postgresql://localhost:5432/postgres
sample.datasource.username=postgres
sample.datasource.password=postgres
sample.datasource.driver-class-name=org.postgresql.Driver
sample.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
sample.jpa.properties.hibernate.default_schema=upload
sample.jpa.hibernate.ddl-auto=create-drop
sample.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
sample.jpa.propertie.hibernate.physical_naming_strategy=com.example.demo.configuration.TableNameStrategy
tableName=dynamictable //table name
Моя сущность Класс
@Entity
@Data
public class Sample {
@Id
private Integer id;
}
с пользовательской конфигурацией базы данных, она не работает должным образом. кто-нибудь может сказать мне, где я делаю ошибки?