Spring + Hibernate + Maven项目出错

时间:2014-12-19 17:25:45

标签: java spring hibernate maven intellij-idea

我的项目中存在问题,没有任何帮助.. 其他类不会显示,因为一切都很好,如何修复此错误?我在Intellij IDEA中编写,创建maven项目,加载spring和hibernate库,还加载mysql连接器和其他库。 我的主要课程:

import DAO.StoreDAO;
import logic.Store;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String[] args){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "applicationContext.xml" }, true);
    StoreDAO studDao = (StoreDAO) context.getBean("dataDao");
    Store data1 = new Store();
    data1.setName("test1");
    data1.setType("test1Type");
    data1.setAddress("test1Address");
    studDao.saveStore(data1);
    System.out.println(studDao.findStore("t%").size());
}
}

的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<context:annotation-config />
<context:component-scan base-package="logic" />

<aop:config>
    <aop:pointcut id="myPointcut"
                  expression="execution(* DAO.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="save*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://localhost:3306/practick</value>
    </property>
    <property name="username">
        <value>root</value>
    </property>
    <property name="password">
        <value></value>
    </property>
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/hibernate.cfg.xml" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
        </props>
    </property>
</bean>

<bean id="dataDao" class="DAO.Impl.StoreHibernateDAO">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

错误列表:

19, 2014 7:07:22 PM org.springframework.context.support.ClassPathXmlApplicationContext         prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e0aca6:     startup date [Fri Dec 19 19:07:22 EET 2014]; root of context hierarchy
груд. 19, 2014 7:07:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader     loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
груд. 19, 2014 7:07:23 PM org.springframework.jdbc.datasource.DriverManagerDataSource     setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
груд. 19, 2014 7:07:23 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.0.1
груд. 19, 2014 7:07:23 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
груд. 19, 2014 7:07:23 PM org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
груд. 19, 2014 7:07:23 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
груд. 19, 2014 7:07:23 PM org.hibernate.cfg.Configuration configure
INFO: configuring from url:     file:/C:/Users/Oleg%20Svyryd/IdeaProjects/untitled/target/classes/hibernate.cfg.xml
груд. 19, 2014 7:07:23 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice':     Cannot resolve reference to bean 'transactionManager' while setting bean property     'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException:     Error creating bean with name 'transactionManager' defined in class path resource     [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean     property 'sessionFactory'; nested exception is     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.MappingException: An AnnotationConfiguration instance is     required to use <mapping clazz="logic.Store"/>
at     org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)...
at Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
... 30 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name    'transactionManager' defined in class path resource [applicationContext.xml]: Cannot resolve    reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is   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.MappingException: An AnnotationConfiguration instance is   required to use <mapping clazz="logic.Store"/>
... 30 more
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use     <mapping clazz="logic.Store"/>

还有很多其他......

1 个答案:

答案 0 :(得分:0)

请确保一些事情

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://localhost/practick</value>
    </property>
    <property name="username">
        <value>root</value>
    </property>
    <property name="password">
        <value></value>
    </property>
</bean>
  1. 确保您输入了密码
  2. 将jdbc:mysql:// localhost / practick更改为jdbc:mysql:// localhost:3306 / practick
  3. 确保您拥有名称&#39; practick&#39;。
  4. 的现有数据库

    此错误基本上来自数据源或SessionFactory,并且它被链接到第一个资源bean。

    txAdvice需要事务管理器,它需要sessionFactory

    你的错误实际上是这一点,显示

    WARNING: Exception encountered during context initialization - cancelling refresh attempt
    

    添加以下内容并检查其是否有效

    Spring支持通过以下方式自动扫描实体类:

    <context:component-scan base-package="your.package.name" />