Home > Back-end >  @Transactional annotation not found in the classpath
@Transactional annotation not found in the classpath

Time:09-05

@Transactional annotation is not being recognized in IntelliJ I try to search and add the Maven dependency but it still not working. Also tried to close, clear app cache and import the project's pom, not working ...

Thanks 4ur help!

code preview

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public class UsuarioDaoImp implements UsuarioDao {
    @Override
    public List<Usuario> getUsuarios() {
        return null;
    }
}

Error on annotation maven dependency add, clicking on the one shown doesn't really add it

pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.potencialaboral</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.22</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

In order to find a library by a class you go to Maven Search and search for fc:[class name], here's a search for @Transactional. Unfortunately there are a lot of versions of each library so the output is too large. But eventually it's possible to find the dependency:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.3.22</version>
</dependency>

Couple of notes:

  1. For @Transactional to work you need to declare Spring's TransactionManager bean. You can check if transactional logic actually works by debugging the code, putting a breakpoint inside your Transactional logic and see if there are transactional proxies in the stack trace.
  2. We don't usually want our Dao/Repository layer to be transactional. Transactions usually start at a Service layer as they may encompass more than one call to Dao/Repository. But you could make sure that a transaction is required to be started on Service layer by putting this onto Dao/Repository classes:
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.MANDATORY)
public class UsuarioDaoImp implements UsuarioDao {}

CodePudding user response:

Without looking at your pom.xml, assuming you have the correct depedency required configured already my suggestion is try to invalidate the Intellij Idea caches, restart the IDE and then reload the maven depedency again.

Edits: By pom.xml you provided looks like you have not import dependency that provide @Transactional annotation, this annotation comes from Spring Transaction dependency

you can manually add spring-tx depedency in your pom or you can use Spring Boot Starter Data JPA which also use this dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Please note that spring boot starter data jpa provides more functionality than handling transaction, it is an abstraction of data access layer for your application in which you can implement in your code easily

  • Related