2012-01-12 117 views
17

我不想拥有hibernate.cfg.xml文件。相反,我想通过我的代码完成所有配置,如下所示。是否需要配置hibernate.cfg.xml文件

private static final SessionFactory factory; 
private static final Properties properties; 

static 
{ 
    properties = new Properties(); 
    properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
    properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/books"); 
    properties.setProperty("hibernate.connection.username", "jhtp7"); 
    properties.setProperty("hibernate.connection.password", "password"); 
    properties.setProperty("hibernate.show_sql", "com.mysql.jdbc.Driver"); 
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 

    factory = new Configuration().setProperties(properties).configure(). 
          buildSessionFactory(); 
} 

我试过上面的方法。但是我面临的问题是,hibernate抛出一个异常说“./hibernate.cfg.xml file missing”。

是否真的必须保留hibernate.cfg.xml文件?

在此先感谢

回答

20

我相信是因为你的configure()打电话给Configuration的对象。尝试删除,休眠不会寻找不存在的文件。基本上你通过properties对象设置所有必需的属性,所以没有必要告诉Hibernate寻找一个hibernate.cfg.xml文件,这正是configure()方法所做的。

10

不,我不认为配置xml是强制性的。要解决您的问题,我认为您需要使用org.hibernate.cfg.Configuration类。看看这个链接:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

基本上,你需要的东西像

Configuration cfg = new Configuration() 
.setProperty("hibernate.dialect", "com.mysql.jdbc.Driver") 
.setProperty("hibernate.connection.datasource", "jdbc:mysql://localhost:3306/books") 
.setProperty("hibernate.order_updates", "true"); 

然后创建SessionFactory的,你刚才说,cfg.buildSessionFactory();

6

不,它不是必须使用hibernate.cfg.xml。只是不要使用.configure()。 如果我们使用.configure(),Hibernate将查找hibernate.cfg.xml。

public class HibernateUtil { 
    private static SessionFactory sessionFactory ; 
    static { 
     Configuration configuration = new Configuration(); 

     configuration.addAnnotatedClass (org.gradle.Person.class); 
     configuration.setProperty("hibernate.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("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 
     configuration.setProperty("hibernate.hbm2ddl.auto", "update"); 
     configuration.setProperty("hibernate.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; 
    } 
} 
+0

如果要在控制台上记录sql查询,请使用:configuration.setProperty(“hibernate.show_sql”,“true”); – Deepak 2015-10-12 16:23:50

+0

那么如何指定映射? – digz6666 2016-08-30 05:13:38

+2

通过这种方式:configuration.addAnnotatedClass(org.gradle.Person.class); – Deepak 2016-08-31 04:07:31

相关问题