在打包应用程序时获取包org.springframework.transaction.annotation不存在错误

时间:2013-03-08 12:25:52

标签: java spring hibernate spring-mvc

我在Spring世界很新。我使用Spring 3.2和Hibernate 4.1.9开发了DAO,但我注意到了一件奇怪的事情。

与Spring相关的所有使用的依赖项属于3.2.1版本,但与 spring-aop 模块相关的除外。对于这个模块,我必须使用3.2.0版本,因为如果我在dao类实现中使用3.2.1,则找不到此导入: org.springframework.transaction.annotation.Transactional

这是我原来的 pom.xml 文件(效果很好):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.andrea.myexample</groupId>
    <artifactId>HibernateOnSpring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>HibernateOnSpring</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Dipendenze di Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>


        <dependency>   <!-- Usata da Hibernate 4 per LocalSessionFactoryBean -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>

        <!-- Dipendenze per AOP -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- Dipendenze per Persistence Managment -->

        <dependency>    <!-- Apache BasicDataSource -->
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>    <!-- MySQL database driver -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.23</version>
        </dependency>

        <dependency>    <!-- Hibernate -->
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.9.Final</version>
        </dependency>

    </dependencies>
</project>

这是我的 PersonDAOImpl 类(实现我的具体DAO的类):

package org.andrea.myexample.HibernateOnSpring.dao;

import java.util.List;

import org.andrea.myexample.HibernateOnSpring.entity.Person;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.springframework.transaction.annotation.Transactional;

public class PersonDAOImpl implements PersonDAO {

    // Factory per la creazione delle sessioni di Hibernate:
    private static SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    // CREATE CRUD Operation:
    @Transactional(readOnly = false)
    public Integer addPerson(Person p) {

        System.out.println("Inside addPerson()");

        Session session = sessionFactory.openSession();

        Transaction tx = null;
        Integer personID = null;

        try {
            tx = session.beginTransaction();

            personID = (Integer) session.save(p);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

        return personID;

    }

    // READ CRUD Operation (legge un singolo record avente uno specifico id):
    public Person getById(int id) {

        System.out.println("Inside getById()");

        Session session = sessionFactory.openSession();

        Transaction tx = null;          
        Person retrievedPerson = null;  

        try {
            tx = session.beginTransaction();
            retrievedPerson = (Person) session.get(Person.class, id);
            tx.commit();
        }catch (HibernateException e) { 
            if (tx != null)                 
                tx.rollback();          
            e.printStackTrace();
        } finally {                 
            session.close();
        }

        return retrievedPerson;
    }

    // READ CRUD Operation (recupera la lista di tutti i record nella tabella):
    @SuppressWarnings("unchecked")
    public List<Person> getPersonsList() {

        System.out.println("Inside getPersonsList()");

        Session session = sessionFactory.openSession();
        Transaction tx = null;
        List<Person> personList = null;

        try {
            tx = session.beginTransaction();
            Criteria criteria = session.createCriteria(Person.class);
            personList = criteria.list();
            System.out.println("personList: " + personList);
            tx.commit();
        }catch (HibernateException e) { 
            if (tx != null)                 
                tx.rollback();          
            e.printStackTrace();
        } finally {
            session.close();
        }
        return personList;
    }

    // DELETE CRUD Operation (elimina un singolo record avente uno specifico id):
    public void delete(int id) {

        System.out.println("Inside delete()");

        Session session = sessionFactory.openSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            Person personToDelete = getById(id);
            session.delete(personToDelete);
            tx.commit();
        }catch (HibernateException e) { 
            if (tx != null)                 
                tx.rollback();          
            e.printStackTrace();
        } finally {
            session.close();
        }

    }

    @Transactional
    public void update(Person personToUpdate) {

        System.out.println("Inside update()");

        Session session = sessionFactory.openSession();
        Transaction tx = null;

        try {
            System.out.println("Insite update() method try");
            tx = session.beginTransaction();
            session.update(personToUpdate);

            tx.commit();
        }catch (HibernateException e) { 
            if (tx != null)                 
                tx.rollback();          
            e.printStackTrace();
        } finally {
            session.close();
        }   

    }

}

问题是,如果我使用 spring-aop 模块的 3.2.1 版本而不是 3.2.0 更改此以这种方式依赖:

    <dependency>   <!-- Usata da Hibernate 4 per LocalSessionFactoryBean -->
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.2.1.RELEASE</version>
    </dependency>

当我运行Maven时---&gt; Maven Install发生编译失败并返回以下错误消息:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/andrea/Documents/ws/HibernateOnSpring/src/main/java/org/andrea/myexample/HibernateOnSpring/dao/PersonDAOImpl.java:[15,49] error: package org.springframework.transaction.annotation does not exist
[ERROR] /home/andrea/Documents/ws/HibernateOnSpring/src/main/java/org/andrea/myexample/HibernateOnSpring/dao/PersonDAOImpl.java:[27,2] error: cannot find symbol
[ERROR]  class PersonDAOImpl
/home/andrea/Documents/ws/HibernateOnSpring/src/main/java/org/andrea/myexample/HibernateOnSpring/dao/PersonDAOImpl.java:[128,2] error: cannot find symbol
[INFO] 3 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.288s
[INFO] Finished at: Fri Mar 08 13:19:23 CET 2013
[INFO] Final Memory: 12M/105M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project HibernateOnSpring: Compilation failure: Compilation failure:
[ERROR] /home/andrea/Documents/ws/HibernateOnSpring/src/main/java/org/andrea/myexample/HibernateOnSpring/dao/PersonDAOImpl.java:[15,49] error: package org.springframework.transaction.annotation does not exist
[ERROR] /home/andrea/Documents/ws/HibernateOnSpring/src/main/java/org/andrea/myexample/HibernateOnSpring/dao/PersonDAOImpl.java:[27,2] error: cannot find symbol
[ERROR] class PersonDAOImpl
[ERROR] /home/andrea/Documents/ws/HibernateOnSpring/src/main/java/org/andrea/myexample/HibernateOnSpring/dao/PersonDAOImpl.java:[128,2] error: cannot find symbol
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Andin PersonDAOImpl 发生时找不到以下导入的类:

org.springframework.transaction.annotation.Transactional;

如果我尝试使用3.2.1版本的spring-orm模块,为什么会出现此问题?如何解决使用它?

3 个答案:

答案 0 :(得分:19)

org.springframework.transactionspring-tx工件提供。将以下内容添加到您的pom.xml并执行maven更新,应该执行以下操作:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

答案 1 :(得分:2)

尝试使用相同版本的所有与spring相关的依赖项。 我猜org.springframework.transaction.annotation.Transactional出现在Spring 3.2.0中。 所以这应该不是问题。包含@Transactionl的spring-tx依赖关系。

答案 2 :(得分:1)

@Transactional注释位于spring-tx模块中。包括版本3.2.1,并始终确保使用所有Spring模块的相同版本。