Hello everyone I am making a web-app and having some problems. I am sharing my codes as well as problems.
and here is my dao
package service;
import java.util.List;
import com.blog.mapperclass.UserData;
public interface UserService {
public void addUser(UserData data);
//public List <UserData> getAllUsers();
}
this is my DAO implementation
package com.blog.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.stereotype.Component;
import com.blog.mapperclass.UserData;
import service.UserService;
@Component
public class Usersdao implements UserService{
SessionFactory session= new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
@Override
public void addUser(UserData data) {
Session ses=session.openSession();
ses.beginTransaction();
ses.save(data);
ses.getTransaction().commit();
System.out.println("user added");
}
}
my request mapping
@RequestMapping("/submit")
public String showSubmit(@ModelAttribute UserData data){
dao.addUser(data);
return "submit";
}
my cfg file
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:8086/blog</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">validate</property>
<mapping class="com.blog.mapperclass.UserData"></mapping>
</session-factory>
</hibernate-configuration>
and here is the error
Constructor threw exception; nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.blog.mapperclass.UserData"/>
and when I use :
SessionFactory session= new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
it gives error
Failed to instantiate [com.blog.dao.Usersdao]: Constructor threw exception; nested exception is java.lang.IncompatibleClassChangeError: Implementing class
And all my jars are updated
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.5-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
Please tell me the some solution for the problem I am newbie.
P.S : here is my pojo class
package com.blog.mapperclass;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity
@Table(name="users")
@Component
public class UserData {
@Id
@Column
private String user_id;
@Column
private String name;
@Column
private String password;
@Column
private String email;
@Column
private String phone;
@Column
private int roll_id;
public UserData(String user_id, String name, String password, String email, String phone, int roll_id) {
super();
this.user_id = user_id;
this.name = name;
this.password = password;
this.email = email;
this.phone = phone;
this.roll_id = roll_id;
}
public UserData(){}
public UserData(String name, String password, String email, String phone) {
this.name = name;
this.password = password;
this.email = email;
this.phone = phone;
}
public UserData(String user_id, String name, String password, String email, String phone) {
super();
this.user_id = user_id;
this.name = name;
this.password = password;
this.email = email;
this.phone = phone;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
and my sql table :