Java 如何为Spring数据JPA查询设置默认模式名?
我想为postgresql设置默认模式。当我尝试设置属性(“hibernate.default_schema”)并用schema prop更新表anotation时,hibernate仍然生成查询,但没有任何模式名称,如下所示Java 如何为Spring数据JPA查询设置默认模式名?,java,spring,hibernate,spring-boot,spring-data-jpa,Java,Spring,Hibernate,Spring Boot,Spring Data Jpa,我想为postgresql设置默认模式。当我尝试设置属性(“hibernate.default_schema”)并用schema prop更新表anotation时,hibernate仍然生成查询,但没有任何模式名称,如下所示 select log0_.id as id1_0_ from log log0_ 当我将表名更改为“dbo.log”时,我可以毫无错误地获得resultSet @Table(name = "dbo.log") 有没有办法为postgresql查询设置默认模式 @Co
select log0_.id as id1_0_ from log log0_
当我将表名更改为“dbo.log”时,我可以毫无错误地获得resultSet
@Table(name = "dbo.log")
有没有办法为postgresql查询设置默认模式
@Configuration
@EnableJpaRepositories(
basePackages = "com.test.repository.postgresql",
entityManagerFactoryRef = "postgresqlEntityManagerFactory",
transactionManagerRef = "postgresqlTransactionManager"
)
public class PostgreSQLConfig {
....
@Bean(name = "postgresqlEntityManagerFactory")
public EntityManagerFactory entityManagerFactory(@Qualifier("postgresqlDatasource") DataSource postgresqlDatasource) {
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.dialect", hibernateDialect);
jpaProperties.setProperty("hibernate.show_sql", hibernateShowSql);
jpaProperties.setProperty("hibernate.ddl-auto", hibernateDDLAuto);
jpaProperties.setProperty("hibernate.naming.physical-strategy", hibernatePhysicalStrategy);
jpaProperties.setProperty("hibernate.default_schema", "dbo");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.POSTGRESQL);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setJpaProperties(jpaProperties);
factory.setPackagesToScan("com.test.entity.postgresql");
factory.setDataSource(postgresqlDatasource);
factory.afterPropertiesSet();
return factory.getObject();
}
}
这是我的实体类
@Entity
@Table(name = "log", schema = "dbo")
public class Log{
....
}
这应该在
数据源的JDBC连接URL中声明。URL模式如下所示:jdbc:postgresql://host:port/database
,其中数据库是模式
如果这还不够,请在URL中设置currentSchema
指定要设置的架构(或由逗号分隔的多个架构)
在搜索路径中。此架构将用于解决不合格问题
此连接上的语句中使用的对象名称
以下是JDBC驱动程序的文档:
如果您使用的是application.properties
spring.datasource.url=jdbc:postgresql://myserver:5432/mydb
spring.jpa.properties.hibernate.default\u schema=myschema
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver类名=org.postgresql.driver
出于好奇,如果使用Spring Boot,为什么要定义自己的EntityManagerFactory
。每个数据库都有自己的配置、实体和存储库。当我更新连接url属性(如“jdbc:p”)时,currentSchema不起作用ostgresql://localhost:5432/mydb?currentSchema=dbo“您确定这里没有将Postgres与SQL Server混合使用吗?dbo听起来像sqlserver。尝试设置currentSchema=mydb。不,我没有混音。我还为postgreSQL创建了一个名为“dbo”的模式。我可以使用@Table(name=“dbo.log”)和连接url“jdbc:p”获取结果ostgresql://localhost:5432/mydb“这也可能与纳明战略有关。我从来没用过这个。我会删除这个,并尝试它只是与网址。您还可以在答案中添加如何配置数据源
。