简体   繁体   English

SessionFactory 在 Hibernate 中返回 null

[英]SessionFactory returning null in Hibernate

I am beginner in Hibernate and facing problem in creation of SessionFactory ,我是 Hibernate 的初学者,在创建SessionFactory时遇到问题,

SessionFactoryProvider.java: SessionFactoryProvider.java:

public class SessionFactoryProvider {

    private static SessionFactory factory;

    public static SessionFactory getFactory() {

        try {
            if (factory == null) {
                Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

                factory = cfg.buildSessionFactory();
            }
        } catch (Exception e) {
            try {
                if (factory == null) {
                    Configuration configuration = new Configuration();
                    // Hibernate settings equivalent to hibernate.cfg.xml's properties
                    Properties property = new Properties();
                    property.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
                    property.put(Environment.URL, "jdbc:mysql://localhost:3306/shopingsite");
                    property.put(Environment.USER, "root");
                    property.put(Environment.PASS, "");
                    property.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
                    property.put(Environment.SHOW_SQL, "true");
                    property.put(Environment.HBM2DDL_AUTO, "update");
                    property.put(Environment.POOL_SIZE, 12);
                    configuration.setProperties(property);
                    configuration.addAnnotatedClass(Category.class);
                    configuration.addAnnotatedClass(User.class);
                    configuration.addAnnotatedClass(Product.class);

                    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                            .applySettings(configuration.getProperties()).build();
                    factory = configuration.buildSessionFactory(serviceRegistry);

                }
            } catch (HibernateException | NullPointerException he) {
                System.out.println("Both factory code Fail");
            }
        }

        return factory;
    }

    public static void shutdow() {
        factory.close();
    }
}

pom.xml: pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany</groupId>
        <artifactId>MavenShopping</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>

        <name>MavenShopping</name>

        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>7.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.4.6.Final</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.16</version>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>8.0</version>
                <type>jar</type>
            </dependency>
        </dependencies>
    </project>

hibernate.cfg.xml hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/shopingsite</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"/>
    <property name="show_sql">true</property>
    <property name="hibernate.connection.autocommit">true</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping class="BeanFile.Category"/>
    <mapping class="BeanFile.Product"/>
    <mapping class="BeanFile.User"/>
  </session-factory>
</hibernate-configuration>

Error which was showing in the log is shown below日志中显示的错误如下所示

Info:   MavenShopping was successfully deployed in 1,530 milliseconds.
Info:   1
Info:   HHH000412: Hibernate Core {5.4.6.Final}
Info:   ******************************************
Info:   HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
WARN:   HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Info:   HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/shopingsite]
Info:   HHH10001001: Connection properties: {user=root, password=****, autocommit=true}
Info:   HHH10001003: Autocommit mode: true
Warning:   StandardWrapperValve[RegistrationServlet]: Servlet.service() for servlet RegistrationServlet threw exception
java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V

How to solve this?如何解决这个问题?

I am beginner in Hibernate and facing problem in creation of SessionFactory ,我是 Hibernate 的初学者,在创建SessionFactory时遇到问题,

SessionFactoryProvider.java: SessionFactoryProvider.java:

public class SessionFactoryProvider {

    private static SessionFactory factory;

    public static SessionFactory getFactory() {

        try {
            if (factory == null) {
                Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

                factory = cfg.buildSessionFactory();
            }
        } catch (Exception e) {
            try {
                if (factory == null) {
                    Configuration configuration = new Configuration();
                    // Hibernate settings equivalent to hibernate.cfg.xml's properties
                    Properties property = new Properties();
                    property.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
                    property.put(Environment.URL, "jdbc:mysql://localhost:3306/shopingsite");
                    property.put(Environment.USER, "root");
                    property.put(Environment.PASS, "");
                    property.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
                    property.put(Environment.SHOW_SQL, "true");
                    property.put(Environment.HBM2DDL_AUTO, "update");
                    property.put(Environment.POOL_SIZE, 12);
                    configuration.setProperties(property);
                    configuration.addAnnotatedClass(Category.class);
                    configuration.addAnnotatedClass(User.class);
                    configuration.addAnnotatedClass(Product.class);

                    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                            .applySettings(configuration.getProperties()).build();
                    factory = configuration.buildSessionFactory(serviceRegistry);

                }
            } catch (HibernateException | NullPointerException he) {
                System.out.println("Both factory code Fail");
            }
        }

        return factory;
    }

    public static void shutdow() {
        factory.close();
    }
}

pom.xml: pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany</groupId>
        <artifactId>MavenShopping</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>

        <name>MavenShopping</name>

        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>7.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.4.6.Final</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.16</version>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>8.0</version>
                <type>jar</type>
            </dependency>
        </dependencies>
    </project>

hibernate.cfg.xml hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/shopingsite</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"/>
    <property name="show_sql">true</property>
    <property name="hibernate.connection.autocommit">true</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping class="BeanFile.Category"/>
    <mapping class="BeanFile.Product"/>
    <mapping class="BeanFile.User"/>
  </session-factory>
</hibernate-configuration>

Error which was showing in the log is shown below日志中显示的错误如下所示

Info:   MavenShopping was successfully deployed in 1,530 milliseconds.
Info:   1
Info:   HHH000412: Hibernate Core {5.4.6.Final}
Info:   ******************************************
Info:   HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
WARN:   HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Info:   HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/shopingsite]
Info:   HHH10001001: Connection properties: {user=root, password=****, autocommit=true}
Info:   HHH10001003: Autocommit mode: true
Warning:   StandardWrapperValve[RegistrationServlet]: Servlet.service() for servlet RegistrationServlet threw exception
java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V

How to solve this?如何解决这个问题?

The problem with your Hibernate SessionFactory has something to do with your environment.您的Hibernate SessionFactory的问题与您的环境有关。 It would appear that your compile time libraries are not the same as your runtime libraries, as a method on one of the classes you use when coding isn't there when the server kicks off.看起来您的编译时库与您的运行时库不同,因为当服务器启动时,您在编码时使用的其中一个类的方法不存在。

java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V

That seems to be the big problem triggering issues with SessionFactory creation .这似乎是触发SessionFactory 创建问题的大问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM