简体   繁体   中英

SessionFactory returning null in Hibernate

I am beginner in Hibernate and facing problem in creation of SessionFactory ,

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:

<?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

<?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 ,

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:

<?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

<?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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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