@TransactionAttribute cannot resolve symbol in spring boot

Michu93 :

I wrote a small method:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void assignDepartment(Long departmentId, String pesel) {
        entityManager.createQuery("UPDATE Employee e set departmentId = :departmentId where e.pesel = :pesel")
                .setParameter("departmentId", departmentId)
                .setParameter("pesel", pesel)
                .executeUpdate();
    }

but I receive an error Cannot resolve symbol TransactionAttribute. Am I missing something in pom.xml?

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.spingframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

Annotation is required otherwise I have: Exception in thread "main" javax.persistence.TransactionRequiredException: Executing an update/delete query

John Smith :

On Spring side it's not TransactionAttribute, it's Transactional. So, your method would like like

@Transactional( propagation = Propagation.REQUIRES_NEW )
public void assignDepartment(Long departmentId, String pesel) {
        entityManager.createQuery("UPDATE Employee e set departmentId = :departmentId where e.pesel = :pesel")
                .setParameter("departmentId", departmentId)
                .setParameter("pesel", pesel)
                .executeUpdate();
}

and both Transactional and Propagation live inside org.springframework.transaction.annotation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related