s2sh-【求助】关于S2SH整合遇到的Error creating bean with name 'sessionFactory'

问题描述

【求助】关于S2SH整合遇到的Error creating bean with name 'sessionFactory'

这是生成的applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
        value="com.mysql.jdbc.Driver">
    </property>
    <property name="url" value="jdbc:mysql://localhost:3306//ideal-workshop"></property>
    <property name="username" value="root"></property>
    <property name="password" value="mysql"></property>
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
import java.sql.Connection;

import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext.xml");
// BasicDataSource dataSource=(BasicDataSource) ap.getBean("dataSource");
// Connection con=(Connection) dataSource.getConnection();
// System.out.println(con);
SessionFactory sf=(SessionFactory) ap.getBean("sessionFactory");
Session session = sf.openSession();
Transaction trans=session.beginTransaction();
trans.begin();
session.createSQLQuery("create table users").executeUpdate();
trans.commit();
}
}

这是我Test类,主要是通过ApplicationContext ap得到dataSource或者sessionFactory实例来测试数据库是否连接正常,但是却爆出无法create bean异常:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error applying BeanValidation relational constraints
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.idealworkshop.test.Test.main(Test.java:20)
Caused by: org.hibernate.HibernateException: Error applying BeanValidation relational constraints
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:306)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 12 more

解决方案

这个问题主要是因为在springxml问价中配置hibernate,没有增加prop javax.persistence.validation.mode 造成的.解决方案:
在下的节点增加
none节点就OK了,自己动手,丰衣足食.

时间: 2024-12-31 08:12:32

s2sh-【求助】关于S2SH整合遇到的Error creating bean with name &amp;#39;sessionFactory&amp;#39;的相关文章

spring-h、s整合问题:No bean named &amp;amp;#39;sessionFactory&amp;amp;#39; is defined

问题描述 h.s整合问题:No bean named 'sessionFactory' is defined 我用的是hibernate和spring整合,在beans.xml写了sessionFactory.在类里面调用@Resource时就报错了 @Resource(name="sessionFactory") public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = s

Error creating bean with name &amp;#39;sessionFactory&amp;#39; defined in file

在hibernate和spring整合时,出现错误如下: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [G:\workspace\s_hibernate\src\applicationContext.xml]: Invocation of

ssh 框架问题 能显示页面,但是出现Error creating bean with n

问题描述 ssh 框架问题 能显示页面,但是出现Error creating bean with n 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error crea

Error creating bean with name &amp;#39;sessionFactory&amp;#39; defined in ServletContext resource [/WEB-INF/applica

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoun

Error creating bean with name &amp;#039;dataSource&amp;#039; defined in ServletContext resource

问题描述 struts-config.xml配置信息<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.d

spring mvc-service层加入@Tractional 后报错error creating bean

问题描述 service层加入@Tractional 后报错error creating bean 我在一个web项目中想加入事务,是采用spring mvc+hibernate的框架配置文件中已经加入 <!-- 配置Hibernate事务管理器 --> <bean id=""transactionManager"" class=""org.springframework.orm.hibernate4.HibernateTran

ssh整合- &amp;amp;#39;sessionFactory&amp;amp;#39; or &amp;amp;#39;hibernateTemplate&amp;amp;#39; is required

问题描述 'sessionFactory' or 'hibernateTemplate' is required 怎么改错????? <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net

spring 错误 error creating bean with name

问题描述 spring 错误 error creating bean with name org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is org.spring

Error creating bean with name &amp;#039;sessionFactory

问题描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in