Java Hibernate中Environment.DEFAULT_模式和数据库的差异

Java Hibernate中Environment.DEFAULT_模式和数据库的差异,java,database,hibernate,jakarta-ee,Java,Database,Hibernate,Jakarta Ee,cfg.setProperty(Environment.DEFAULT_SCHEMA,“exampleDB”) 与 jdbc:mysql://127.0.0.1:3306/exampleDB 连接URL中的默认架构和数据库是否完全不同? 我注意到,如果省略Environment.DEFAULT_SCHEMA的设置,并执行以下操作: Configuration cfg = new Configuration(); System.out.println("Default schema:"+cfg.g


cfg.setProperty(Environment.DEFAULT_SCHEMA,“exampleDB”)


jdbc:mysql://127.0.0.1:3306/exampleDB

连接URL中的默认架构和数据库是否完全不同?
我注意到,如果省略
Environment.DEFAULT_SCHEMA
的设置,并执行以下操作:

Configuration cfg = new Configuration();
System.out.println("Default schema:"+cfg.getProperty(Environment.DEFAULT_SCHEMA));
sessionFactory = cfg.configure().buildSessionFactory();
控制台打印:
默认模式:null

那么,
Environment.DEFAULT_SCHEMA
的用途是什么,以及与连接数据库的区别呢

参考: 和

我的理解是: 在hibernate.cfg.xml中,您可以设置

<property name="hibernate.default_schema">exampleDB</property>
如何创建供以后使用的默认模式

new SchemaExport(config).create(true,true); //First parameter (true) creates new schema
编辑: 和另一个参考:

提供对在属性对象中传递的配置信息的访问

Hibernate有两个属性作用域:

Factory-level properties may be passed to the SessionFactory when it instantiated. Each instance might have different property values. If no properties are specified, the factory calls Environment.getProperties().
System-level properties are shared by all factory instances and are always determined by the Environment properties. 
唯一的系统级属性是

hibernate.jdbc.use_streams_for_binary
hibernate.cglib.use_reflection_optimizer 
通过调用System.getProperties()填充环境属性,然后从名为/hibernate.properties(如果存在)的资源填充环境属性。系统属性覆盖hibernate.properties中指定的属性。

SessionFactory由以下属性控制。属性可以是系统属性、在名为/hibernate.Properties的资源中定义的属性或传递给配置的java.util.Properties的实例。buildSessionFactory()

请参阅: 和

我的理解是: 在hibernate.cfg.xml中,您可以设置

<property name="hibernate.default_schema">exampleDB</property>
如何创建供以后使用的默认模式

new SchemaExport(config).create(true,true); //First parameter (true) creates new schema
编辑: 和另一个参考:

提供对在属性对象中传递的配置信息的访问

Hibernate有两个属性作用域:

Factory-level properties may be passed to the SessionFactory when it instantiated. Each instance might have different property values. If no properties are specified, the factory calls Environment.getProperties().
System-level properties are shared by all factory instances and are always determined by the Environment properties. 
唯一的系统级属性是

hibernate.jdbc.use_streams_for_binary
hibernate.cglib.use_reflection_optimizer 
通过调用System.getProperties()填充环境属性,然后从名为/hibernate.properties(如果存在)的资源填充环境属性。系统属性覆盖hibernate.properties中指定的属性。


SessionFactory由以下属性控制。属性可以是系统属性、在名为/hibernate.Properties的资源中定义的属性或传递给Configuration.buildSessionFactory()的java.util.Properties实例。当DBA在单个数据库中创建多个架构时,将使用默认架构属性。我在postgres中看到过这种情况,每个人都使用一个数据库URL,但在该URL下,每个应用程序都有单独的模式


通过使用默认模式属性,它允许您将模式名称从实体中删除。如果您针对不支持模式的HSqlDB运行测试,并且针对使用模式的DB进行部署,则这一点特别有用。空值只意味着默认返回到DB默认模式。

当DBA在单个数据库中创建多个模式时,将使用默认模式属性。我在postgres中看到过这种情况,每个人都使用一个数据库URL,但在该URL下,每个应用程序都有单独的模式


通过使用默认模式属性,它允许您将模式名称从实体中删除。如果您针对不支持模式的HSqlDB运行测试,并且针对使用模式的DB进行部署,则这一点特别有用。空值只意味着默认返回DB默认模式。

设置hibernate属性会导致hibernate限定它发出的sql中的所有表名,即,而不是

select count(*)
from mytable
它会发出

select count(*)
from myschema.mytable
到数据库


附加到连接字符串的效果是特定于数据库的。某些数据库(例如Oracle)不支持在连接字符串中指定默认架构。()

设置hibernate属性会导致hibernate限定它发出的sql中的所有表名,即,而不是

select count(*)
from mytable
它会发出

select count(*)
from myschema.mytable
到数据库


附加到连接字符串的效果是特定于数据库的。某些数据库(例如Oracle)不支持在连接字符串中指定默认架构。()

好的,
环境.DEFAULT_模式
代表什么?我的理解2:在运行时为您提供读/写访问hibernate.cfg DEFAULT_模式属性。假设您编写了一个应用程序,并希望检查一个条件是否为true,然后您希望将默认的_模式设置为a,如果为false,您希望将其设置为b。我也在和你一起学习,希望能有所帮助:)好的,
环境.DEFAULT\u SCHEMA
代表什么?我的理解2:在运行时为你提供读/写访问hibernate.cfg DEFAULT\u SCHEMA属性。假设您编写了一个应用程序,并希望检查一个条件是否为true,然后您希望将默认的_模式设置为a,如果为false,您希望将其设置为b。我也在和你一起学习,希望能有所帮助:)