【问题标题】:Setting properties programmatically in Hibernate在 Hibernate 中以编程方式设置属性
【发布时间】:2011-08-29 19:14:26
【问题描述】:

如何确保从 hibernate.cfg.xml 加载所有属性,然后以编程方式添加其他属性?我看到了下面的代码 sn-p 但它看起来像是一个全新的配置,而不是现有配置的添加。

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

c.setProperty("hibernate.connection.username", "abc" );
c.setProperty("hibernate.connection.password", "defgh629154" ); 

【问题讨论】:

标签: hibernate


【解决方案1】:

您展示的代码 sn-p 就是您所需要的。只需使用现有配置,而不是创建新配置。

如果实例化配置的不是您(而是例如 spring),您需要扩展创建它的类。

【讨论】:

  • 啊。谢谢!我很困惑,因为 Hibernate 没有在任何地方的任何 bean 中列出。它实际上是通过DWR间接使用的,spring没有实例化它。
【解决方案2】:

您的代码 sn-p 应该从类路径的根目录加载 hibernate.cfg.xml,然后以编程方式添加或覆盖配置属性。因此,请确保您所谓的“现有hibernate.cfg.xml”位于类路径的根目录上。

如果你的“现有hibernate.cfg.xml”不在类路径的根目录下,而是在某个包中,你可以通过在configure()中指定包路径来加载它,喜欢

Configuration config = new Configuration();
config.configure("package1/package2/hibernate.cfg.xml");
config.setProperty("hibernate.connection.username", "update" );
config.setProperty("hibernate.connection.password", "defgh629154" ); 

【讨论】:

  • 关键是c.configure()hibernate.cfg.xml资源加载配置。 OP似乎没有意识到这一点,这个答案澄清了他的困惑。因此,这应该被投票为正确答案。
  • @Binil,这个答案并没有澄清我的困惑。我标记为“答案”的答案澄清了我的困惑。
【解决方案3】:
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
  private static SessionFactory sessionFactory;

  static {
    Configuration configuration = new Configuration();

    configuration.addAnnotatedClass (org.gradle.Person.class);
    configuration.setProperty("connection.driver_class","com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");                                
    configuration.setProperty("hibernate.connection.username", "root");     
    configuration.setProperty("hibernate.connection.password", "root");
    configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
    configuration.setProperty("hibernate.hbm2ddl.auto", "update");
    configuration.setProperty("show_sql", "true");
    configuration.setProperty(" hibernate.connection.pool_size", "10");

    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    sessionFactory = configuration.buildSessionFactory(builder.build());
  }

  public static SessionFactory getSessionFactory() {
     return sessionFactory;
  }
} 

使用.configure() 使Hibernate 寻找hibernate.cfg.xml 文件。因此,如果您不想使用hibernate.cfg.xml 文件,请不要使用.configure()

【讨论】:

    【解决方案4】:

    这比我想象的工作正常

    public class HibernateUtil {
    
        private static final SessionFactory sessionFactory;
    
        static {
            try {
                // Create the SessionFactory from standard (hibernate.cfg.xml) 
                // config file.
    
    
                Properties c = new Properties();
                c.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                c.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
                c.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydiscou_billing?zeroDateTimeBehavior=convertToNull");
                c.setProperty("hibernate.connection.username", "root");
                c.setProperty("hibernate.connection.password", "123");
                c.setProperty("hibernate.connection.autoReconnect", "true");
    
                c.setProperty("connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
                c.setProperty("c3p0.min_size", "5");
                c.setProperty("c3p0.max_size", "20");
                c.setProperty("c3p0.timeout", "1800");
                c.setProperty("c3p0.max_statements", "100");
                c.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");
    
    
    
    
                sessionFactory = new AnnotationConfiguration().setProperties(c).configure().buildSessionFactory();
            } catch (Throwable ex) {
                // Log the exception. 
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }
    
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    }
    

    【讨论】:

    【解决方案5】:

    也许你可以像这样创建你的配置:

    Configuration cfg = new Configuration();
    cfg.addResource("Hibernate.cfg.xml");
    

    然后应用您的特定属性设置。

    我假设您确实想自己实例化您的配置。如果不是,您需要从实例化它的任何东西中获取它,例如如果您使用的是 Spring 的 LocalSessionFactoryBean。

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 2017-08-05
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      相关资源
      最近更新 更多