注入自动连接的依赖项失败(spring + hibernate + gwt)

时间:2011-04-21 15:14:57

标签: java hibernate spring jpa

嘿大家好!我正在尝试使用spring + gwt + hibernate开发一个简单的应用程序。我有spring + gwt工作,但是当我实现hibernate时我得到了这个错误

 00:00:00.018 [WARN] Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'telekanalDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.persistence.EntityManagerFactory com.karq.saatekava.server.dao.TelekanalDAO.entityManagerFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to configure EntityManagerFactory:
javax.validation.ValidationException: Unable to find a default provider     at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)  at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:342)   at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:106)  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:616)     at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:118)  at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1674)     at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1624)   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1415)   at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)   at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)   at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)   at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:278)    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:362)    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)  at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)    at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:92)    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)    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:291)  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)  at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:844)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:786)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)  at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)  at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)   at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)   at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:461)     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)  at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.Server.doStart(Server.java:222)    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:565)   at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:494)     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1058)    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:800)     at com.google.gwt.dev.DevMode.main(DevMode.java:304)

我遵循了本教程:http://www.javacodegeeks.com/2010/05/gwt-2-spring-3-jpa-2-hibernate-35.html

这是我的telekanalDAO课程

package com.karq.saatekava.server.dao;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.karq.saatekava.shared.dto.TelekanalDTO;


@Repository("telekanalDAO")
public class TelekanalDAO extends JpaDAO<Long, TelekanalDTO> {

    @Autowired
    EntityManagerFactory entityManagerFactory;

    @PostConstruct
    public void init(){
        super.setEntityManagerFactory(entityManagerFactory);
    }

}

这是我的bean文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:component-scan base-package="com.karq.saatekava"/>

    <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

    <task:executor id="myExecutor" pool-size="5"/>

    <task:scheduler id="myScheduler" pool-size="10"/>

    <tx:annotation-driven/>

    <bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="MyPersistenceUnit"/>
    </bean>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

</beans>

希望有人可以提供帮助

2 个答案:

答案 0 :(得分:1)

对我而言,您在DTO上使用了一些JSR-303注释,而您在类路径中缺少验证提供程序:

javax.validation.ValidationException: Unable to find a default provider

Hibernate Validator添加到Classpath。

答案 1 :(得分:0)

听起来你错过了META-INF/persistence.xml