在Hibernate中以编程方式设置属性

Kyl*_*leM 17 hibernate

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

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

c.setProperty("hibernate.connection.username", "abc" );
c.setProperty("hibernate.connection.password", "defgh629154" ); 
Run Code Online (Sandbox Code Playgroud)

Boz*_*zho 14

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

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


Ken*_*han 8

您应该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" ); 
Run Code Online (Sandbox Code Playgroud)

  • 关键点是`c.configure()`从`hibernate.cfg.xml`资源加载配置.OP似乎没有意识到这一点,这个答案澄清了他的困惑.因此,这应该被选为正确的答案. (2认同)

小智 6

这比我想象的要好

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Dee*_*pak 5

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;
  }
} 
Run Code Online (Sandbox Code Playgroud)

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