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

时间:2011-05-20 16:07:01

标签: hibernate

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

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

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

5 个答案:

答案 0 :(得分:14)

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

如果您不是实例化配置的人(例如,弹簧),则需要扩展创建它的类。

答案 1 :(得分: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" ); 

答案 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;
    }
}

答案 3 :(得分:4)

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 :(得分:1)

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

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

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

我假设您确实希望自己实例化您的配置。如果不是,你需要从实例化它的任何东西中得到它,例如Spring的LocalSessionFactoryBean,如果你正在使用它。