Hibernate中Environment.DEFAULT_SCHEMA和数据库的区别
Hibernate的区别是什么cfg.setProperty(Environment.DEFAULT_SCHEMA, "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 ofcfg.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当 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.
参考:
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 中你可以设置;
或者您可以在执行时构建 hibernate.cfg 文件;
如何创建默认模式供以后使用;
编辑:
和另一个参考:
http://docs.jboss.org/hibernate /core/3.3/api/org/hibernate/cfg/Environment.html
提供对 Properties 对象中传递的配置信息的访问。
Hibernate 有两个属性范围:
唯一的系统级属性是
通过调用 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;
or you can construct the hibernate.cfg file at execution time;
how you can create default schema for later use;
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:
The only system-level properties are
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()
设置 hibernate 属性会导致 hibernate 限定它发出的 sql 中的所有表名,即而不是
发送
到数据库。
附加到连接字符串的效果是特定于数据库的。某些数据库(例如Oracle)不支持在连接字符串中指定默认模式。 (来源)
Setting the hibernate property causes hibernate to qualify all table names in the sql it emits, i.e. instead of
it would send
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)