Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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
Java 我能';无法在Hibernate 5.1.0中获取SessionFactory。获取SessionFactory obj时,我的代码在行中中断_Java_Hibernate - Fatal编程技术网

Java 我能';无法在Hibernate 5.1.0中获取SessionFactory。获取SessionFactory obj时,我的代码在行中中断

Java 我能';无法在Hibernate 5.1.0中获取SessionFactory。获取SessionFactory obj时,我的代码在行中中断,java,hibernate,Java,Hibernate,错误: 1) AdminModel.java-模型类 2) HibernateUtil.java简化了Hibernate数据库连接 3) AdminDAO.java-u guyz知道这些是什么…我将省去解释的痛苦…哦,是的…我已经用这个bug忍受了好几天的痛苦…tryna debug…我还有最后期限要完成。。。如果你能帮助我,那将是一件大事 public class AdminModel { private int adminID; private String username

错误:

1) AdminModel.java-模型类

2) HibernateUtil.java简化了Hibernate数据库连接

3) AdminDAO.java-u guyz知道这些是什么…我将省去解释的痛苦…哦,是的…我已经用这个bug忍受了好几天的痛苦…tryna debug…我还有最后期限要完成。。。如果你能帮助我,那将是一件大事

public class AdminModel {
    private int adminID;
    private String username;
    private String password;

    public int getAdminID() {
        return adminID;
    }
    public void setAdminID(int adminID) {
        this.adminID = adminID;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}
HibernateUtil.java

adminao.java

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ekatabookstoreDB</property>
        <property name="connection.username">xyz</property>
        <property name="connection.password">xyz</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>   

      <!-- Enable Hibernate's automatic session context management -->  
        <property name="current_session_context_class">thread</property>   

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">create</property> -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="ekatabookstore.hbm.xml" /> 

    </session-factory>

</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
     <class name="com.ekatabookstore.layer.service.model.AdminModel" table="admin">
        <id name="adminID" type="integer" column="id_admin">
            <generator class="assigned" />           
        </id>

        <property name="username" type="string" column="username" not-null="true" />
        <property name="password" type="string" column="password" not-null="true" />        
    </class>
</hibernate-mapping>

com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/ekatabookstoreDB
xyz
xyz
1.
org.hibernate.dialogue.mysqldialogue
线
org.hibernate.cache.internal.NoCacheProvider
真的
更新
hbn.properties hiberNateCfgFileName=hibernate.cfg.xml

ekatabookstore.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ekatabookstoreDB</property>
        <property name="connection.username">xyz</property>
        <property name="connection.password">xyz</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>   

      <!-- Enable Hibernate's automatic session context management -->  
        <property name="current_session_context_class">thread</property>   

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">create</property> -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="ekatabookstore.hbm.xml" /> 

    </session-factory>

</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
     <class name="com.ekatabookstore.layer.service.model.AdminModel" table="admin">
        <id name="adminID" type="integer" column="id_admin">
            <generator class="assigned" />           
        </id>

        <property name="username" type="string" column="username" not-null="true" />
        <property name="password" type="string" column="password" not-null="true" />        
    </class>
</hibernate-mapping>

如果您使用的是spring,请在sessionfactory上添加autowire或从应用程序上下文获取autowire。您可以使用以下代码

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware{

   private static ApplicationContext context;

   public static ApplicationContext getApplicationContext() {
      return context;
   }

   @Override
   public void setApplicationContext(ApplicationContext ac)
           throws BeansException {
       context = ac;
   }
}
您可以在代码中的任何位置使用此类,如

ApplicationContextProvider.getApplicationContext().getBean("sessionFactory");

如果您使用的是spring,请在sessionfactory上添加autowire或从应用程序上下文中获取它。您可以使用以下代码

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware{

   private static ApplicationContext context;

   public static ApplicationContext getApplicationContext() {
      return context;
   }

   @Override
   public void setApplicationContext(ApplicationContext ac)
           throws BeansException {
       context = ac;
   }
}
您可以在代码中的任何位置使用此类,如

ApplicationContextProvider.getApplicationContext().getBean("sessionFactory");

请尝试以下代码来创建sessionfactory

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings from hibernate.cfg.xml
    .build();
try {
    sessionFactory = new MetadataSources( registry).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
    StandardServiceRegistryBuilder.destroy( registry );
}

希望这有帮助。

请尝试以下代码来创建sessionfactory

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings from hibernate.cfg.xml
    .build();
try {
    sessionFactory = new MetadataSources( registry).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
    StandardServiceRegistryBuilder.destroy( registry );
}

希望这有帮助。

尝试调试的静态块HibernateUtil@anupambhusari:是的,里面的东西应该是问题所在,但是在调试过程中经过了几天。我用了2-3种不同的方法来处理这段代码。你可以看到你自己在上面的同一个班级里。除此之外,我认为Jboss官方网站上也有关于2种不同方法或3种猜测的信息。疾病已经成为我生命中的痛苦。所以我不认为,这段代码就是问题所在。我是一个冬眠新手,所以我错过了一些我忽略的基本东西。请同时查看我的xml文件。THNK You哦,我忘了提到,,非常重要的一件事……我已经制定了一种代码生成方法,仅供参考……但那是在maven中——使用同一版本的Hibernate 5.1.0做依赖性的事情……而同样的代码现在在非maven中被弃用……这种方法是使用注释的……现在这里的要求是使用xml映射……我需要这样做方法…架构方法你们看到了…所以,若你们能帮我这个方法…试着调试的静态块HibernateUtil@anupambhusari:是的,里面的东西应该是问题所在,但是在调试过程中经过了几天。我用了2-3种不同的方法来处理这段代码。你可以看到你自己在上面的同一个班级里。除此之外,我认为Jboss官方网站上也有关于2种不同方法或3种猜测的信息。疾病已经成为我生命中的痛苦。所以我不认为,这段代码就是问题所在。我是一个冬眠新手,所以我错过了一些我忽略的基本东西。请同时查看我的xml文件。THNK You哦,我忘了提到,,非常重要的一件事……我已经制定了一种代码生成方法,仅供参考……但那是在maven中——使用同一版本的Hibernate 5.1.0做依赖性的事情……而同样的代码现在在非maven中被弃用……这种方法是使用注释的……现在这里的要求是使用xml映射……我需要这样做方法…架构方法你知道…所以请你帮我这个方法…似乎代码减少了很多…但我还没有加入。虽然它在我的清单上…我的方法可能会对一些人有帮助…或者可能对我自己有一些帮助…我在进入春季后不会再与你联系…但是,无论如何…谢谢你:):)看起来很有帮助代码减少了…但我还不喜欢。虽然在我的列表上…我的代码可能会对一些人有所帮助…或者在一些tym之后对我自己…我会在我进入春季后再次与你联系…但是安威…谢谢你:):)->Lyk我在评论部分已经说过了…我甚至使用了来自官方文档网站的代码。。。我觉得它不管用…我想你看到我上面评论的篇幅会觉得很懒…我已经试过这个了。。。。谢谢你的辛苦研究…谢谢:)->Lyk我之前在评论部分说过…我甚至使用了官方文档网站的代码。。。我觉得它不管用…我想你看到我上面评论的篇幅会觉得很懒…我已经试过这个了。。。。感谢您为研究付出的辛劳……谢谢:)