当前位置: 首页 > 知识库问答 >
问题:

Hibernate出现错误,因为在没有internet连接的情况下,“Hibernate.dialect”未设置时,“对方言解析信息的访问不能为null”

寇桐
2023-03-14

当有互联网连接时,Hibernate工作正常。但当没有互联网连接时,它会给我

当'hibernate.dialect'未设置时,对DialectResolutionInfo的访问不能为空

错误。我认为我的应用程序在运行时正在尝试访问Internet连接。我得到的详细错误如下:-

我的pom。xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.pos.admin.admin_module</groupId>
    <artifactId>admin_module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>admin_module</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

<!--    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build> -->

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

<!--        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.5.Final</version>
        </dependency> -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> 
            <version>4.3.5.Final</version> </dependency> -->

        <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> 
            <version>5.2.2.Final</version> </dependency> -->

        <!-- <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> 
            <version>2.2.4</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> 
            <artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency> -->
    </dependencies>
</project>

我的hibernate配置(hibernate.cfg.xml)文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost/point_of_sales</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">
        </property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>
        <!-- 
      <property name="hbm2ddl.auto">update</property>  -->

        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Category"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.ProductHeader"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.PurchaseDetail"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.PurchaseHeader"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.SalesDetail"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.SalesHeader"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Settings"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Unit"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.User"/>
        <mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Supplier"/>

<!--         <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.Supplier"/>
        <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.PurchaseHeader"/>
        <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.ProductHeader"/>
        <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.Category"/>
        <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.Unit"/> -->
  <!--       <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.User"/> -->

        <!-- <mapping class="com.pos.admin.admin_module.admin_module.main.Cart1"/> --> 

    </session-factory>
</hibernate-configuration>

HibernateUtil类,我在其中创建配置和会话对象

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate-annotation.cfg.xml
            Configuration configuration = new Configuration();
            try{
            configuration.configure("hibernate.cfg.xml");
            }catch(Exception e){
                System.out.println("Exception while handling configureation file :" + e.getMessage());
                System.out.println("asfsdaf " + configuration);
            }
            System.out.println("Hibernate Annotation Configuration loaded");

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            System.out.println("Hibernate Annotation serviceRegistry created");

            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            return sessionFactory;
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        if(sessionFactory == null) sessionFactory = buildSessionFactory();
        return sessionFactory;
    }

    public static void closeSessionFactory(){
        if(!sessionFactory.isClosed()){
            System.out.println("Closing SessionFactory");
            sessionFactory.close();
        }
    }
}

我想解决这个问题,因为互联网连接不能总是可用。非常感谢。

共有3个答案

申屠泳
2023-03-14

一旦能够建立JDBC连接,您的Hibernate版本就能够自动配置Dialect。如果您无法建立JDBC连接,那可能是个问题。

茹康裕
2023-03-14

尝试从此中删除SessionFactory:

SessionFactory  sessionFactory = configuration.buildSessionFactory(serviceRegistry);

成为:

   sessionFactory = configuration.buildSessionFactory(serviceRegistry);

或者像这样添加这个buildSessionFactory()

  private static SessionFactory sessionFactory = buildSessionFactory();
刘骏祥
2023-03-14

要脱机使用hibernate,您必须更改xml映射和配置文件中的dtd。确保hibernate jar在类路径中

for xml mapping file
    <!DOCTYPE hibernate-mapping SYSTEM 
        "classpath://org/hibernate/hibernate-mapping-3.0.dtd">

对于配置文件:

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

http://www.journaldev.com/2959/how-to-configure-hibernate-cfg-xml-to-work-offline

 类似资料: