-2

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;
  }
 }
 
  
 
}
here is my hibernate mapping

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

<%@ 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>
exception.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:-

<%@ 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>
success.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>
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>
I'm using maven in this project.

The error is given at the line:

SessionFactory sf = c.buildSessionFactory();
in the database connection file EmpDao.java.
hlhp
  • 17
  • 5

1 Answers1

0

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();
Joginder Malik
  • 455
  • 5
  • 12
  • which hibernate jars you are using ? Use the below url to download and add jars of required folder only. http://sourceforge.net/projects/hibernate/files/hibernate4/4.0.0.Final/hibernate-release-4.0.0.Final.zip/download i.e. dom4j-1.6.1.jar, antlr-2.7.7.jar, commons-collections-3.2.1.jar, jboss-transaction-api_1.1_spec-1.0.0.Final.jar, javassist-3.12.1.GA.jar, classmate-0.5.4.jar, hibernate-jpa-2.0-api-1.0.1.Final.jar, jandex-1.0.3.Final.jar, jboss-logging-3.1.0.CR2.jar, hibernate-commons-annotations-4.0.1.Final.jar, hibernate-core-4.0.0.Final.jar – Joginder Malik Apr 16 '15 at 12:28
  • i'm using maven pluggin...means means when i using maven build then it autometically diwnloading all the struts and hibernate and mysql dependencies that mentioned in pom.xml which appeares when i convert my project to maven..so i dont know why the exception is appearing can u solve this ....then it will bw very graefull..t – Sumanta Banerjee Apr 16 '15 at 13:30
  • you are using hibernate-core two times, remove them antlr does not have remove entitymanager i found that you have to comment all hibernate specific jars in pom and use the 11 jars which i mentioned above. – Joginder Malik Apr 16 '15 at 13:58
  • hey tell me your email id , i will send you correct pom.xml as of now. – Joginder Malik Apr 16 '15 at 14:23
  • ok ya my email id is** sumantabnrj@yahoo.co.in** pls it'll be very helpfull thanks.... – Sumanta Banerjee Apr 17 '15 at 06:49
  • now replace your pom.xml file with which i sent over to you.And use above code which i answered for creating sessionFactory and session.if got error then tell me on email – Joginder Malik Apr 17 '15 at 07:05
  • still finding exception i try to paste it:- – Sumanta Banerjee Apr 17 '15 at 11:03
  • `java.lang.AbstractMethodError at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:295) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) at com.actions.EmpDao.register(EmpDao.java:50) at com.actions.EmpAction.execute(EmpAction.java:39) session factory problem` – Sumanta Banerjee Apr 17 '15 at 11:05
  • and the line in which it finding exception is same as above:-` sf = configuration.buildSessionFactory(serviceRegistry);` – Sumanta Banerjee Apr 17 '15 at 11:07