Hibernate中Environment.DEFAULT_SCHEMA和数据库的区别

发布于 2024-12-04 19:23:45 字数 634 浏览 1 评论 0原文

Hibernate的区别是什么
cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");


jdbc:mysql://127.0.0.1:3306/exampleDB

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

Configuration cfg = new Configuration();
System.out.println("Default schema:"+cfg.getProperty(Environment.DEFAULT_SCHEMA));
sessionFactory = cfg.configure().buildSessionFactory();

控制台打印:Default schema:null

那么Environment.DEFAULT_SCHEMA有什么用处以及与连接DB的区别呢?

What is the difference in Hibernate of
cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");

with
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/exampleDB</property>

Is default schema and DB in the connection URL completely different things?
I noticed that if I omit the setting of Environment.DEFAULT_SCHEMA and do the following:

Configuration cfg = new Configuration();
System.out.println("Default schema:"+cfg.getProperty(Environment.DEFAULT_SCHEMA));
sessionFactory = cfg.configure().buildSessionFactory();

The console prints: Default schema:null.

So what is the use of Environment.DEFAULT_SCHEMA and the difference with the connecting DB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远山浅 2024-12-11 19:23:45

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

通过使用默认模式属性,它允许您将模式名称保留在实体之外。如果您针对不支持模式的 HSqlDB 运行测试并且针对使用模式的数据库进行部署,则这特别有用。具有空值仅意味着它默认返回到数据库默认模式。

The default schema property is used when your DBA creates multiple schemas within a single database. I've seen this with postgres where everyone uses one database URL but under that there are separate schemas for each application.

By using the default schema property it allows you to keep the schema names off your entities. This is particularly useful if you're running tests against HSqlDB which does not support schemas and you deploy against a DB that is using schemas. Having a null value just means it defaults back to the DB default schema.

2024-12-11 19:23:45

参考:
http://www.youtube.com/watch?v=ReAZmA83Myg&feature=lated

http://www.java-forums.org /database/1467-variables-hibernate-cfg-xml-file.html

我的理解:
在 hibernate.cfg.xml 中你可以设置;

<property name="hibernate.default_schema">exampleDB</property>

或者您可以在执行时构建 hibernate.cfg 文件;

cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");

如何创建默认模式供以后使用;

new SchemaExport(config).create(true,true); //First parameter (true) creates new schema

编辑:
和另一个参考:
http://docs.jboss.org/hibernate /core/3.3/api/org/hibernate/cfg/Environment.html

提供对 Properties 对象中传递的配置信息的访问。

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 实例

refer to:
http://www.youtube.com/watch?v=ReAZmA83Myg&feature=related
and
http://www.java-forums.org/database/1467-variables-hibernate-cfg-xml-file.html

what I understand:
in hibernate.cfg.xml you can setup;

<property name="hibernate.default_schema">exampleDB</property>

or you can construct the hibernate.cfg file at execution time;

cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");

how you can create default schema for later use;

new SchemaExport(config).create(true,true); //First parameter (true) creates new schema

edit:
and another reference:
http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/cfg/Environment.html

Provides access to configuration info passed in Properties objects.

Hibernate has two property scopes:

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. 

The only system-level properties are

hibernate.jdbc.use_streams_for_binary
hibernate.cglib.use_reflection_optimizer 

Environment properties are populated by calling System.getProperties() and then from a resource named /hibernate.properties if it exists. System properties override properties specified in hibernate.properties.

The SessionFactory is controlled by the following properties. Properties may be either be System properties, properties defined in a resource named /hibernate.properties or an instance of java.util.Properties passed to Configuration.buildSessionFactory()

逆流 2024-12-11 19:23:45

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

select count(*)
from mytable

发送

select count(*)
from myschema.mytable

到数据库。

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

Setting the hibernate property causes hibernate to qualify all table names in the sql it emits, i.e. instead of

select count(*)
from mytable

it would send

select count(*)
from myschema.mytable

to the database.

The effect of appending to the connection string is database specific. Some databases (e.g. Oracle) do not support specifying the default schema in the connection string. (source)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文