Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否必须有hibernate.cfg.xml文件进行配置_Hibernate_Configuration - Fatal编程技术网

是否必须有hibernate.cfg.xml文件进行配置

是否必须有hibernate.cfg.xml文件进行配置,hibernate,configuration,Hibernate,Configuration,我不想要hibernate.cfg.xml文件。相反,我希望通过我的代码完成所有配置,如下所示 private static final SessionFactory factory; private static final Properties properties; static { properties = new Properties(); properties.setProperty("hibernate.connection.driver_class", "com

我不想要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文件丢失”

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


提前感谢

不,我认为配置xml不是必需的。为了解决您的问题,我认为您需要使用org.hibernate.cfg.Configuration类。请查看此链接:

基本上,你需要

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()

我相信这是因为您对
configure
对象的
configure()
调用。尝试删除该文件,hibernate将不会查找不存在的文件。基本上,您是通过
properties
对象来设置所有必需的属性,因此不需要告诉Hibernate查找
Hibernate.cfg.xml
文件,这正是
configure()
方法所做的。

不,使用Hibernate.cfg.xml不是强制性的。只是不要使用
.configure()
。 如果我们使用
.configure()
,Hibernate将查找Hibernate.cfg.xml


基本上,您是通过properties对象设置所有必需的属性,因此不需要告诉Hibernate查找Hibernate.cfg.xml文件,这正是configure()方法所做的。使用hibernate.cfg.xml不是强制性的。不要使用.configure()。

static {
    try {
        Properties prop= new Properties();
        prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
        prop.setProperty("hibernate.connection.username", "root");
        prop.setProperty("hibernate.connection.password", "");
        prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");

        concreteSessionFactory = new AnnotationConfiguration()
       .addPackage("com.concretepage.persistence")
               .addProperties(prop)
               .addAnnotatedClass(User.class)
               .buildSessionFactory();
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }

如果要在控制台上记录sql查询,请使用:configuration.setProperty(“hibernate.show_sql”,“true”);那么如何指定映射呢?通过这种方式:configuration.addAnnotatedClass(org.gradle.Person.class);谢谢,我做到了。我在寻找像addPackages或addAnnotatedPackages这样的东西。
static {
    try {
        Properties prop= new Properties();
        prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
        prop.setProperty("hibernate.connection.username", "root");
        prop.setProperty("hibernate.connection.password", "");
        prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");

        concreteSessionFactory = new AnnotationConfiguration()
       .addPackage("com.concretepage.persistence")
               .addProperties(prop)
               .addAnnotatedClass(User.class)
               .buildSessionFactory();
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }