在调用buildsessionfactory方法时发现Hibernate异常

时间:2015-04-16 08:38:59

标签: struts2

我发现异常: - [java.lang.NoSuchMethodError:org.hibernate.internal.CoreMessageLogger.debugf(Ljava / lang / String; I)V] 同时调用buildsessionfactory方法。 对于这个例外,我还没有能够连接数据库(mysql)。 这是我的代码: - 行动档案: - 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";
		}
	}  
}



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



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;
		}
	}
	
		
	
}



 这是我的hibernate映射

文件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>
&#13;
&#13;
&#13; 这是我的hibernate配置文件hibernate.cfg.xml: -

&#13;
&#13;
<?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>
&#13;
&#13;
&#13; struts.xml文件: -

&#13;
&#13;
<?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>	
&#13;
&#13;
&#13; web.xml中: -

&#13;
&#13;
<?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>
&#13;
&#13;
&#13; 的error.jsp: -

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13; exception.jsp: -

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13; index.jsp的: -

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13; 的success.jsp: -

&#13;
&#13;
<%@ 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>
&#13;
&#13;
&#13; 和pom.xml: -

&#13;
&#13;
<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>
&#13;
&#13;
&#13; 我在这个项目中使用了maven。

错误在以下行给出:

&#13;
&#13;
SessionFactory sf = c.buildSessionFactory();
&#13;
&#13;
&#13; 在数据库连接文件EmpDao.java中。

1 个答案:

答案 0 :(得分:0)

这是正常工作的代码你可以试试。

    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();