【问题标题】:How to use hibernate.properties file instead of hibernate.cfg.xml如何使用 hibernate.properties 文件而不是 hibernate.cfg.xml
【发布时间】:2014-07-17 11:58:18
【问题描述】:

我正在尝试使用 Hibernate 连接到 servlet 中的数据库。我已经读到我们可以使用 hibernate.cfg.xml 或 hibernate.properties 文件来配置会话。对我来说,它与 xml 一起使用。现在,当我尝试使用属性而不是 xml 时,它不起作用。 据说 hibernate.cfg.xml not found。但是我没有提到要使用 xml 文件,事实上我已经删除了那个 xml 文件。

请帮助我。 如果我做错了什么,请纠正我。

【问题讨论】:

  • 请分享您的 hibernate.properties 文件。

标签: java xml database hibernate properties


【解决方案1】:

这段代码默认会调用hibernate.cfg.xml:

Configuration configuration = new Configuration().configure();

而且这段代码默认会调用hibernate.properties:

Configuration configuration = new Configuration();

希望对你有帮助。

【讨论】:

  • 我需要什么!谢谢。
【解决方案2】:

根据我从hibernate的理解,最好的办法是在hibernate.cfg.xml文件中定义映射,在hibernate.properties中定义其他配置。

另一种配置方法是在名为 hibernate.cfg.xml 的文件中指定完整配置。此文件可用作 hibernate.properties 文件的替代品,或者如果两者都存在,则可以覆盖属性。

一旦您必须调整 Hibernate 缓存,hibernate.cfg.xml 也更方便。您可以选择使用 hibernate.properties 或 hibernate.cfg.xml。两者是等价的。

您可以在以下链接中阅读更多相关信息:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

【讨论】:

  • 如何替换属性文件中的
【解决方案3】:

如果您使用的是hibernate.properties,请删除.configure()。下面的代码是 HibernateUtil 会话工厂实现。

private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        ServiceRegistry serviceRegistry
            = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        configuration.addAnnotatedClass(LookUpModel.class);
        return configuration
                .buildSessionFactory(serviceRegistry);
    }catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException("There is issue in hibernate util");
    }
}

hibernate.properites 文件

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password = passsword
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

【讨论】:

    【解决方案4】:

    如果您使用的是来自 servletdatabase,那么您应该在服务器中定义一个 DataSource 并将一个休眠属性指向该位置通过您现在可能正在使用的所有其他休眠属性来定义所有内容。

    这样做的好处是允许您独立于您的应用程序定义连接池和其他与连接相关的参数。

    例如,您的生产环境的数据库密码可能与您的测试和开发环境不同。

    【讨论】:

      【解决方案5】:
      public class FactoryConfiguration {
      
          private static FactoryConfiguration factoryConfiguration;
          private final SessionFactory sessionFactory;
      
          private FactoryConfiguration(){
              Properties properties = new Properties();
              try {
                  properties.load(new FileInputStream("hibernate.properties"));
              }catch (IOException e) {
                  e.printStackTrace();
              }
              Configuration configuration = new Configuration()
              .addProperties(properties)
                      .addAnnotatedClass(Student.class);
              sessionFactory = configuration.buildSessionFactory();
          }
      
          public static FactoryConfiguration getInstance(){
              return (factoryConfiguration == null) ? factoryConfiguration = new FactoryConfiguration() : factoryConfiguration;
          }
      
          public Session getSession(){
              return sessionFactory.openSession();
          }
      }
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案6】:

      您可以添加注释实体类,如下所示。 这对我有用。

      Properties properties = new Properties();
         try{
             properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("hibernate.properties"));
         }catch (Exception e){
             System.out.println("cannot load properties file.");
         }
          sessionFactory = new Configuration().mergeProperties(properties).addAnnotatedClass(Customer.class).buildSessionFactory();
      

      “hibernate.propeties”是具有休眠配置的文件,请记住正确地提供您的休眠属性文件位置。

      【讨论】:

        猜你喜欢
        • 2011-09-22
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 2019-05-07
        相关资源
        最近更新 更多