【问题标题】:Difference of Environment.DEFAULT_SCHEMA and database in HibernateHibernate 中 Environment.DEFAULT_SCHEMA 和数据库的区别
【发布时间】:2011-09-17 07:53:45
【问题描述】:

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


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

连接 URL 中的默认架构和数据库是否完全不同?
我注意到如果我省略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有什么区别呢?

【问题讨论】:

    标签: java database hibernate jakarta-ee


    【解决方案1】:

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

    通过使用默认架构属性,它允许您将架构名称与您的实体隔离开来。如果您针对不支持模式的 HSqlDB 运行测试并且您针对使用模式的数据库进行部署,这将特别有用。有一个空值只是意味着它默认回到数据库默认模式。

    【讨论】:

      【解决方案2】:

      参考: http://www.youtube.com/watch?v=ReAZmA83Myg&feature=relatedhttp://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

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

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

      【讨论】:

      • 好的,Environment.DEFAULT_SCHEMA 代表什么?
      • 我的理解2:在运行时为您提供 hibernate.cfg default_schema 属性的读/写访问权限。假设您编写了一个应用程序并想检查条件是否为真,那么您想将 default_schema 设置为 a,如果为假,您想将其设置为 b。好吧,我也在和你一起学习,所以希望对你有帮助:)
      【解决方案3】:

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

      select count(*)
      from mytable
      

      它会发送

      select count(*)
      from myschema.mytable
      

      到数据库。

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

      【讨论】:

        猜你喜欢
        • 2011-11-17
        • 2021-07-12
        • 2013-02-06
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多