I am new to spring and hibernate as well as to stackoverflow A developing ecommerce project using spring hibernate maven creating two file one for frontend and 2nd for backend after creating two file i am adding dependency of backend into frontend pom.ml file
I am using java to create sessionfactory ,datasource and transaction manager here is the code I dont understand what mistake i am doing
package com.ecom.Config;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import
org.springframework.transaction.annotation.EnableTransactionManagement;
import com.ecom.Model.CustomerRegistration;
@Configuration
@ComponentScan("com.ecom")
@EnableTransactionManagement
public class AnnotationConfigApplicationContext {
@Bean(name = "dataSource")
public DataSource getH2DataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:h2:tcp://localhost/~/Ecommerce");
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "create");
return properties;
}
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new
LocalSessionFactoryBuilder(dataSource);
sessionBuilder.addProperties(getHibernateProperties());
sessionBuilder.addAnnotatedClass(CustomerRegistration.class);
return sessionBuilder.buildSessionFactory();
}
@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory
sessionFactory) {
HibernateTransactionManager transactionManager = new
HibernateTransactionManager(sessionFactory);
return transactionManager;
}
}
Contoller
package com.ecom.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ecom.DAO.CustomerRegDAO;
import com.ecom.Model.CustomerRegistration;
@Controller
public class CustomerRegController
{
@Autowired
private CustomerRegDAO customerRegDAO ;
@RequestMapping("/registrationForm")
public String Registration(Model theModel)
{
CustomerRegistration theCustomerRegistration = new
CustomerRegistration();
theModel.addAttribute("customer", theCustomerRegistration);
return "registration-form";
}
@RequestMapping("/saveCustomer")
public String saveCustomer(@ModelAttribute("customer")
CustomerRegistration theCustomerRegistration)
{
customerRegDAO.saveCustomer(theCustomerRegistration);
return "index";
}
}
DAO
package com.ecom.DAOImplementation;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.ecom.DAO.CustomerRegDAO;
import com.ecom.Model.CustomerRegistration;
@Repository("CustomerRegDAO")
@Transactional
public class CustomerRegDAOImpl implements CustomerRegDAO {
private SessionFactory sessionFactory;
@Transactional
public void saveCustomer(CustomerRegistration theCustomerRegistration) {
Session currentsession = sessionFactory.getCurrentSession();
currentsession.saveOrUpdate(theCustomerRegistration);
}
}
Error:
Type Exception Report
Message Servlet.init() for servlet [spring] threw exception
Description The server encountered an unexpected condition that prevented
it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet.init() for servlet [spring] threw
exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase
.java:475)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogVa
lve.java:651)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
Root cause
org.springframework.beans.factory.BeanCreationException: Error creating
bean
with name 'sessionFactory' defined in
com.ecom.Config.AnnotationConfigApplicationContext: Bean instantiation via
factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw
exception; nested exception is java.lang.NoClassDefFoundError: Could not
initialize class
org.hibernate.annotations.common.reflection.java.JavaReflectionManager
java.lang.NoClassDefFoundError: Could not initialize class
org.hibernate.annotations.common.reflection.java.JavaReflectionManager
The exception is indicating that the class JavaReflectionManager
is not found. This class is present in hibernate-commons-annotations
jar file. Can you add this in your pom.xml
and try running it again?
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.2.Final</version>
</dependency>
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.