在调用 buildsessionfactory 方法时发现休眠异常 [英] Hibernate exception found in calling buildsessionfactory method

查看:48
本文介绍了在调用 buildsessionfactory 方法时发现休眠异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现异常:-[java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V]在调用 buildsessionfactory 方法时.对于这个异常,我一直无法连接到数据库(mysql).这是我的代码:-动作文件:-EmpAction.java:-

I'm finding the exception :-[java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V] while calling the buildsessionfactory method. For this exception I haven't been able to connect with the database(mysql). Here is my code:- Action file :- EmpAction.java:-

package techmyguru.actions;



import com.opensymphony.xwork2.ActionSupport;
//import com.opensymphony.xwork2.ModelDriven;
//import com.opensymphony.xwork2.Preparable;

public class EmpAction extends ActionSupport {
	//Employee emp;
	private int id;
	private String password;
	private String name;
	private String email;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
    public String execute()
	{
		try{
			int i=EmpDao.register(this);
			if(i==1)
			{
			return "success";
			}
			else
			{
				return "error";
			}
		}
		catch(Throwable t)
		{
			t.toString();
			return "fail";
		}
	}  
}

这是我的数据库连接文件:-

here is my database connection file:-

package techmyguru.actions;

//import java.util.List;

//import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class EmpDao {
	

	public static int  register(EmpAction e) {
		int i=0;
		Configuration c=null;
		SessionFactory sf=null;
		Session session=null;
		Transaction tr=null;
		try
		{
			try
			{
		c=new Configuration().configure("hibernate.cfg.xml");
			}catch(Throwable t)
			{
				System.out.println("configuration problem");
				t.printStackTrace();
			}
			try{
		sf = c.buildSessionFactory();
			}catch(Throwable t)
			{
				System.out.println("sessionfactory problem");
				t.printStackTrace();
			}
			try{
		session=sf.openSession();
			}catch(Throwable t)
			{
				System.out.println("session opening problem");
				t.printStackTrace();
			}
			try{
		tr=session.beginTransaction();
			}catch(Throwable t)
			{
				System.out.println("transaction opening problem");
				t.printStackTrace();
			}
			try{
		tr.begin();
			}catch(Throwable t)
			{
				System.out.println("transaction begining problem");
				t.printStackTrace();
			}
			try{
		i=(Integer)session.save(e);
			}catch(Throwable t)
			{
				System.out.println("transaction saving problem");
				t.printStackTrace();
			}
			try{
		tr.commit();
			}catch(Throwable t)
			{
				System.out.println("transaction commiting problem");
				t.printStackTrace();
			}
		session.close();
		return i;
		}
		catch(Throwable t)
		{
			t.toString();
			return i;
		}
	}
	
		
	
}

这是我的休眠映射

文件 Employee.hbm.xml:-

file Employee.hbm.xml:-

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 23, 2013 8:24:43 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="techmyguru.actions.EmpAction" table="employee">
        <id name="id">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="email" />
        </property>
    </class>
</hibernate-mapping>

这是我的休眠配置文件 hibernate.cfg.xml:-

here is my hibernate configuration file hibernate.cfg.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/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:3306/test</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>
	<property name="connection.pool_size">10</property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="show_sql">true</property>
	<mapping resource="techmyguru/actions/Employee.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

struts.xml 文件:-

struts.xml file:-

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<include file="struts-default.xml" />

	<package name="default" extends="struts-default">
		<action name="EmpAction" class="techmyguru.actions.EmpAction" method="execute">
			<result name="success">success.jsp</result>
			<result name="error">error.jsp</result>
			<result name="fail">exception.jsp</result>
		</action>
	</package>
</struts>	

web.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Struts2Hibernate1</display-name>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>struts.devMode</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

error.jsp:-

error.jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
operation failed.....
</body>
</html>

异常.jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
exception occured....
</body>
</html>

index.jsp:-

index.jsp:-

<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="EmpAction" method="post">
<s:textfield label="Enter name" name="name" value=""/>
<s:password label="Enter password" name="password" value=""/>
<s:textfield label="Enter email" name="email" value=""/>
 <s:submit value="submit"/>
</s:form>


</body>
</html>

成功.jsp:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
operation successfull.......!!!
</body>
</html>

和 pom.xml:-

and 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>Struts2Hibernate1</groupId>
  <artifactId>Struts2Hibernate1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
 <dependency>
  <groupId>junit</groupId> 
  <artifactId>junit</artifactId> 
  <version>3.8.1</version> 
  <scope>test</scope> 
  </dependency>
 <dependency>
  <groupId>commons-lang</groupId> 
  <artifactId>commons-lang</artifactId> 
  <version>2.4</version> 
  </dependency>
 <dependency>
  <groupId>jstl</groupId> 
  <artifactId>jstl</artifactId> 
  <version>1.2</version> 
  </dependency>
 <dependency>
  <groupId>javax.servlet</groupId> 
  <artifactId>javax.servlet-api</artifactId> 
  <version>3.0.1</version> 
  <scope>provided</scope> 
  </dependency>
 <!-- 		<dependency>
			<groupId>org.brickred</groupId>
			<artifactId>socialauth-filter</artifactId>
			<version>[2.4,)</version>
		</dependency>
		 
		<dependency>
    		<groupId>javax.servlet</groupId>
    		<artifactId>servlet-api</artifactId>
    		<version>2.4</version>
    		<type>jar</type>
   			<scope>provided</scope>
		</dependency>
		 
		<dependency>
    		<groupId>javax.servlet.jsp</groupId>
    		<artifactId>jsp-api</artifactId>
    		<version>2.1</version>
    		<type>jar</type>
    		<scope>provided</scope>
		</dependency>
		
		<dependency>
  			<groupId>javax.servlet</groupId>
  			<artifactId>javax.servlet-api</artifactId>
  			<version>3.1.0</version>
  			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jni</artifactId>
			<version>8.0.8</version>
		</dependency>
		
  --> 
 <!--  
		<dependency>
    		<groupId>org.apache.struts</groupId>
    		<artifactId>struts2-junit-plugin</artifactId>
    		<version>2.2.3</version>
		</dependency>
		
  --> 
 <dependency>
  <groupId>org.apache.struts</groupId> 
  <artifactId>struts2-core</artifactId> 
  <version>2.3.16.3</version> 
  </dependency>
 <dependency>
  <groupId>org.apache.struts</groupId> 
  <artifactId>struts-taglib</artifactId> 
  <version>1.3.10</version> 
  </dependency>
 <dependency>
  <groupId>org.apache.struts</groupId> 
  <artifactId>struts-core</artifactId> 
  <version>1.3.10</version> 
 <exclusions>
 <exclusion>
  <groupId>antlr</groupId> 
  <artifactId>antlr</artifactId> 
  </exclusion>
  </exclusions>
  </dependency>
 <dependency>
  <groupId>org.hibernate</groupId> 
  <artifactId>hibernate-core</artifactId> 
  <version>4.3.7.Final</version> 
  </dependency>
  <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.1.Final</version>
            <classifier>tests</classifier>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.1.0.CR2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
 <dependency>
  <groupId>mysql</groupId> 
  <artifactId>mysql-connector-java</artifactId> 
  <version>5.1.30</version> 
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>

<!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>

<!-- optional -->

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-osgi</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-proxool</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-infinispan</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>5.0.0.Beta1</version>
</dependency>
  </dependencies>
  
 <repositories>
 <repository>
  <id>sonatype-oss-public</id> 
  <url>https://oss.sonatype.org/content/groups/public/</url> 
 <releases>
  <enabled>true</enabled> 
  </releases>
  </repository>
  </repositories>
</project>

我在这个项目中使用了 maven.

I'm using maven in this project.

该行给出了错误:

SessionFactory sf = c.buildSessionFactory();

在数据库连接文件 EmpDao.java 中.

in the database connection file EmpDao.java.

推荐答案

这是工作代码,你可以试试.

This is working code You can try it.

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;
    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");

    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    // process your business logic.
    session.getTransaction().commit();

这篇关于在调用 buildsessionfactory 方法时发现休眠异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆