How to validate Data in Service Layer? - spring-boot

I've got a problem which I haven't been able to solve.
I am currently building a Rest Api with Spring Boot and I want to validate my User Entity inside of the service layer. I have tried different approaches, but nothing worked out for now. In my Test no Exception gets thrown when it gets to the create method.
Here is my current code:
User Entity
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank
#Email(message = "User needs a valid Mail.")
#Column(unique = true, nullable = false)
private String mail;
#NotBlank
#Column(nullable = false)
private String lastName;
#NotBlank
#Column(nullable = false)
private String firstName;
}
User Service
#Service
#RequiredArgsConstructor
public class UserService implements IUserService {
private final UserRepository userRepository;
#Override
public User create(#Valid User user) {
user.setId(null);
User createdUser = userRepository.save(user);
return createdUser;
}
My Test
#SpringBootTest
class UserServiceTest {
#Mock
UserRepository userRepository;
#InjectMocks
UserService userService;
#Test
void createUserFailsBecauseNoFirstName() {
User testUser = new User("test#mail.de", "LastName", "FirstName");
when(userRepository.save(any(User.class))).thenReturn(testUser);
testUser.setFirstName(null);
Assertions.assertThrows(ConstraintViolationException.class,
() -> userService.create(testUser));
}
pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rest</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
What I tried:
#Validated for the Service Class (tutorial I followed)
#Validated additionally at Method Level
removing #Validated & #Validate and use Validator instead, but it doesn't get injected (followed that tutorial)

Because you are using #InjectMocks to get the UserService , it is not a bean that is managed by Spring since #InjectMocks is a Mockito annotation and know nothing about Spring and does not know how to get a bean from Spring container. So you are actually testing a non-spring UserService bean now and hence the validation does not takes place.
Change to use #Autowired to get the UserService bean from Spring container , and use #MockBean to replace the UserRepository bean in the Spring container with a mocked version. Also you need to add #Validated on the UserService too :
#Service
#Validated
public class UserService implements IUserService {
}
#SpringBootTest
class UserServiceTest {
#MockBean
UserRepository userRepository;
#Autowired
UserService userService;
#Test
void createUserFailsBecauseNoFirstName() {
User testUser = new User("test#mail.de", "LastName", "FirstName");
when(userRepository.save(any(User.class))).thenReturn(testUser);
testUser.setFirstName(null);
Assertions.assertThrows(ConstraintViolationException.class,
() -> userService.create(testUser));
}
}

Not really the solution that I was searching for, but I couldn't get it to work with Annotations even in a new Spring Boot Project. So I now used a Validator in my Service and let it validate my User.
Service
#Service
#RequiredArgsConstructor
public class UserService implements IUserService {
private final Validator validator;
private final UserRepository userRepository;
#Override
public User create(User user) {
user.setId(null);
// new Validation
Set<ConstraintViolation<T>> violations = validator.validate(objectToValidate);
if (!violations.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (ConstraintViolation<T> constraintViolation : violations) {
sb.append(constraintViolation.getMessage());
}
throw new ResourceValidationException("Error occurred: " + sb.toString());
}
User createdUser = userRepository.save(user);
return createdUser;
}
Test
#SpringBootTest
class UserServiceTest {
#Mock
UserRepository userRepository;
#Autowired
Validator validator;
UserService userService = new UserService(validator, userRepository);
#Test
void createUserFailsBecauseNoFirstName() {
User testUser = new User("test#mail.de", "LastName", "FirstName");
when(userRepository.save(any(User.class))).thenReturn(testUser);
testUser.setFirstName(null);
Assertions.assertThrows(ConstraintViolationException.class,
() -> userService.create(testUser));
}
And since I always got a validator bean which didn't validate (so my tests didn't throw the expected error) and I didn't want to create a ValidatorFactory I also added a Configuration which holds a ValidatorBean.
Config
#Configuration
public class AppConfig {
#Bean
public Validator defaultValidator() {
return new LocalValidatorFactoryBean();
}
}

Related

Duplicate Key Error on saving Reactor Mongo result

I'm seeing the following error when attempting to query and update some records to MongoDB using reactive streams:
org.springframework.dao.DuplicateKeyException: E11000 duplicate key error collection: testtrans.submission index: _id_ dup key: { _id: ObjectId('600b10b2fbac4f4483af3e67') }; nested exception is com.mongodb.MongoWriteException
I'm not sure what I'm doing wrong; How do I go about querying and saving results to the database using reactive mongo in a single transaction?
My service class:
package com.example.reactivemongotransaction.service;
import com.example.reactivemongotransaction.dto.Submission;
import com.example.reactivemongotransaction.repository.SubmissionRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
#Slf4j
#Service
public class SubmissionService {
#Autowired
private SubmissionRepository submissionRepository;
public Mono<Submission> saveSubmission(Submission submission) {
log.debug("saving {}", submission);
return submissionRepository.save(submission);
}
#Transactional
public Flux<Submission> lockSubmissions() {
log.debug("setting all locked");
Flux<Submission> submissionFlux = submissionRepository.findAllByLockedFalse();
return submissionFlux
.map(submission -> submission.setLocked(true))
.flatMap(submission -> submissionRepository.save(submission));
}
}
My config:
#Configuration
#EnableMongoAuditing
#EnableTransactionManagement
public class MongoConfiguration {
#Bean
public ReactiveTransactionManager transactionManager(ReactiveMongoDatabaseFactory dbFactory) {
return new ReactiveMongoTransactionManager(dbFactory);
}
}
Controller:
#RestController
#RequestMapping("/submissions")
public class SubmissionController {
#Autowired
private SubmissionService submissionService;
#PostMapping
public Mono<Submission> saveSubmission(final #RequestBody Submission submission) {
return submissionService.saveSubmission(submission);
}
#GetMapping("/lockall")
public Flux<Submission> lockAll() {
return submissionService.lockSubmissions();
}
}
Model:
#ToString
#Getter
#Setter
#Accessors(chain = true)
#Document(collection = "submission")
#TypeAlias("payload")
public class Submission implements Persistable<String> {
#Id
private String id;
#Field("role_name")
#Indexed(unique = true)
private String role;
#CreatedDate
private ZonedDateTime created;
#LastModifiedDate
private ZonedDateTime updated;
private Boolean deleted;
private Boolean enabled;
private boolean locked;
#Override
#JsonIgnore
public boolean isNew() {
if(getCreated() == null)
return true;
else
return false;
}
}
Repository:
public interface SubmissionRepository extends ReactiveMongoRepository<Submission, String> {
Flux<Submission> findAllByLockedFalse();
}
Main class:
#EnableReactiveMongoRepositories
#SpringBootApplication
public class ReactivemongotransactionApplication {
public static void main(String[] args) {
SpringApplication.run(ReactivemongotransactionApplication.class, args);
}
}
application.yml:
spring:
data:
mongodb:
uri: 'mongodb://localhost:27017/testtrans'
server:
port: 8280
Maven 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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>reactivemongotransaction</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reactivemongotransaction</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.6</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My request is GET http://localhost:8280/submissions/lockall which return 500. And the logs show errors e.g.:
com.mongodb.MongoWriteException: E11000 duplicate key error collection: testtrans.submission index: _id_ dup key: { _id: ObjectId('600b10b2fbac4f4483af3e67') }
at com.mongodb.internal.async.client.AsyncMongoCollectionImpl.lambda$executeSingleWriteRequest$9(AsyncMongoCollectionImpl.java:1075) ~[mongodb-driver-core-4.1.1.jar:na]
The issue was related to the #CreatedDate not being set (see #EnableMongoAuditing and #CreatedDate Auditing not working in Spring Boot 2.4.3).
Resolved by reverting spring-boot-starter-parent to 2.3.5.RELEASE.

created repository method is returning wrong output like packagename.classname.table (com.blogconduitapi.entity.Users#6a3258a4)

I am new lerner of spring boot, i am creating a project after inserting user data into database when i am fetching data from table using method "User findByEmail(String email) it should return user object but it returning something like "com.blogconduitapi.entity.Users#5e2f219c".
All the codes are mentioned below
this is entity class
package com.blogconduitapi.entity;
import javax.persistence.*;
#Entity
#Table(name = "users")
public class Users {
#Id
#GeneratedValue
private int id;
private String name;
private String email;
private String password;
private boolean isEnabled;
public Users() {
}
public Users(String name, String email, String password, boolean isEnabled) {
this.name = name;
this.email = email;
this.password = password;
this.isEnabled = isEnabled;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
}
this is controller
package com.blogconduitapi.controller;
import com.blogconduitapi.entity.Users;
import com.blogconduitapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class UserController {
#Autowired
UserService userService;
#PostMapping(value = "/register")
public String registerUser(#RequestBody Users users) {
Users existingUsers1 = userService.findUser(users.getEmail());
users.setEnabled(true);
userService.createUser(users);
return "user save successfully";
}
}
this is UserService
package com.blogconduitapi.service;
import com.blogconduitapi.entity.Users;
public interface UserService {
Users findUser(String email);
void createUser(Users users);
}
this is repository
package com.blogconduitapi.repository;
import com.blogconduitapi.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UserRepository extends JpaRepository<Users, Integer> {
Users findByEmail(String email);
}
5.this is serviceImpl
package com.blogconduitapi.service.impl;
import com.blogconduitapi.entity.Users;
import com.blogconduitapi.exception.UserDefinedException;
import com.blogconduitapi.repository.UserRepository;
import com.blogconduitapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class UserServiceImpl implements UserService {
#Autowired
UserRepository userRepository;
#Override
public Users findUser(String email) {
Users users = userRepository.findByEmail(email);
System.out.println("{{{{{{{{{}}}}}}}}}}} " + users);
if (users != null) throw new UserDefinedException("mail already exist");
return users;
}
#Override
public void createUser(Users users) {
if (users.getName().isEmpty() || users.getEmail().isEmpty() || users.getPassword().isEmpty()) {
throw new UserDefinedException("There are some required fields are missing");
}
userRepository.save(users);
}
}
application.property
spring.datasource.url= jdbc:postgresql://localhost:5432/apidb
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=update
7.this is pom.xml
<?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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>blogconduitapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>blogconduitapi</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jdbc -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The method is working it's just the issue with print. Override toString() method in your User class and try printing the user again. Java has no idea how to convert your User to string and print it in format you would like otherwise.

Field bookRepository in com.code.service.BookServiceImpl required a bean of type 'com.myAppp.code.respository.BookRepository' that could not be found

I am receiving an error when I try to build/run a SpringBoot application as follows:
Field bookRepository in com.myApp.code.service.BookServiceImpl required a bean of type 'com.myApp.code.respository.BookRepository' that could not be found.
The repository in question is:
package com.myApp.code.respository;
import com.myApp.code.model.Book;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface BookRepository extends CrudRepository<Book, Long> {
}
And in the service class I have the following:
package com.myApp.code.service;
import com.myApp.code.model.Book;
import com.myApp.code.respository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class BookServiceImpl implements BookService {
#Autowired
private BookRepository bookRepository;
#Override
public void list() {
//return bookRepository.findAll();
for (Book book : bookRepository.findAll()) {
System.out.println(book.getTitle());
}
}
In the controller concerned I have:
#Controller
#RequestMapping(value="book")
public class BookController {
#Autowired
private BookService bookService;
#Autowired
private PersonService personService;
#Autowired
private BookValidator bookValidator;
private final Logger LOG = LoggerFactory.getLogger(getClass().getName());
public BookController() {
}
// Displays the catalogue.
#RequestMapping(value="/catalogue", method=RequestMethod.GET)
public String index(Model model) {
LOG.info(BookController.class.getName() + ".catalogue() method called.");
// Populate catalogue.
bookService.list();
// model.addAttribute("books", books);
// Set view.
return "/catalogue";
}
And in the Application.java file I have:
#SpringBootApplication
#EnableJpaRepositories
#ComponentScan(basePackages="com.myApp.code")
public class Application {
private final Logger LOG = LoggerFactory.getLogger(getClass().getName());
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
The Book class is:
#Entity
#Table(name="BOOK")
public class Book implements Serializable {
// Fields.
#Id
#Column(name="id", unique=true, nullable=false)
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Column(name="AUTHOR", nullable=false, length=50)
private String author;
#Column(name="TITLE", nullable=false, length=100)
private String title;
#Column(name="DESCRIPTION", nullable=false, length=500)
private String description;
#Column(name="ONLOAN", nullable=false, length=5)
private String onLoan;
#ManyToOne(fetch=FetchType.EAGER, targetEntity = Person.class)
#JoinColumn(name="Person_Id", nullable=true)
private Person person;
My Maven POM file is:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Can anyone tell me why I get this message? Class BookRepository exists after all.
Spring boot will only look for repositories, entities and components within the same package or a subpackage of the main class (Application). You already added the #ComponentScan to point to the other package, but you should also add the package to both #EntityScan and #EnableJpaRepositories, for example:
#SpringBootApplication
#EnableJpaRepositories("com.myApp.code") // Add this
#EntityScan("com.myApp.code") // Add this
#ComponentScan(basePackages="com.myApp.code")
public class Application {
// ...
}
This is also being mentioned in JpaRepository not implemented/injected when in separate package from componentscan.
Alternatively, as mentioned in the comments, you can put the main class in com.myApp.code itself.
Place your Application class in the com.myApp.code package and not a sub package. – M. Deinum
By doing that, you can remove all three annotations:
#SpringBootApplication // Other annotations can be removed
public class Application {
// ...
}

spring boot mail cannot find JavaMailSender to autowire

Just trying to run the following test.
I am getting NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] when I run it.
What am i missing?
pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
Test class :
#RunWith(SpringRunner.class)
public class SendEmailTest {
#Autowired
private JavaMailSender javaMailService;
#Test
public void testSendEmail() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
String msgText = "This is your <b>mail message</b> from the <h3>Java Test</h3> !";
mailMessage.setTo("xxx.yyy#zzzz.com");
mailMessage.setSubject("A test from Java");
mailMessage.setText(msgText);
javaMailService.send(mailMessage);
}
}
Try this,
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = YourApplication.class)
public class SendEmailTest {
#Autowired
private JavaMailSender javaMailService;
}
In your configuaration class,
#Bean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setProtocol("SMTP");
javaMailSender.setHost("127.0.0.1");
javaMailSender.setPort(25);
return javaMailSender;
}

Spring Boot + Spring Data JPA + Transactions not working properly

I have created a Spring Boot application using 1.2.0 version with spring-boot-starter-data-jpa and I am using MySQL.
I have configured my MySQL properties in application.properties file correctly.
I have a simple JPA Entity, Spring Data JPA Repository and a Service as follows:
#Entity
class Person
{
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
//setters & getters
}
#Repository
public interface PersonRepository extends JpaRepository<Person, Integer>{
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
#Service
#Transactional
class PersonService
{
#Autowired PersonRepository personRepository;
#Transactional
void save(List<Person> persons){
for (Person person : persons) {
if("xxx".equals(person.getName())){
throw new RuntimeException("boooom!!!");
}
personRepository.save(person);
}
}
}
I have the following JUnit test:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
#Autowired
PersonService personService;
#Test
public void test_logging() {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person(null,"abcd"));
persons.add(new Person(null,"xyz"));
persons.add(new Person(null,"xxx"));
persons.add(new Person(null,"pqr"));
personService.save(persons);
}
}
The expectation here is it should not insert any records into PERSON table as it will throw Exception while inserting 3rd person object.
But it is not getting rolled back, first 2 records are getting inserted and committed.
Then I thought of quickly try with JPA EntityManager.
#PersistenceContext
private EntityManager em;
em.save(person);
Then I am getting javax.persistence.TransactionRequiredException: No transactional EntityManager available Exception.
After googling for sometime I encounter this JIRA thread https://jira.spring.io/browse/SPR-11923 on the same topic.
Then I updated the Spring Boot version to 1.1.2 to get Spring version older than 4.0.6.
Then em.save(person) working as expected and Transaction is working fine (means it is rollbacking all the db inserts when RuntimeException occurred).
But even with Spring 4.0.5 + Spring Data JPA 1.6.0 versions transactions are not working when personRepository.save(person) is used instead of em.persist(person).
It seems Spring Data JPA repositories are committing the transactions.
What am I missing? How to make Service level #Transactional annotations work?
PS:
Maven pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sivalabs</groupId>
<artifactId>springboot-data-jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-data-jpa</name>
<description>Spring Boot Hello World</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.0.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.sivalabs.springboot.Application</start-class>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
Application.java
#EnableAutoConfiguration
#Configuration
#ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Change Transactional annotation to
#Transactional(rollbackFor=Exception.class)
From comment by #m-deinum:
Make your PersonService public as well as the method you are calling.
This seems to have done the trick for several users. The same thing is also covered in this answer, citing manual saying:
When using proxies, you should apply the #Transactional annotation
only to methods with public visibility. If you do annotate protected,
private or package-visible methods with the #Transactional annotation,
no error is raised, but the annotated method does not exhibit the
configured transactional settings. Consider the use of AspectJ (see
below) if you need to annotate non-public methods.
I tested this with SpringBoot v1.2.0 and v1.5.2 and all is working as expected, you don't need to use entity manager neither #Transactional(rollbackFor=Exception.class).
I could not see which part you misconfigured, all seems fine at first glance, so I am just posting all my config as reference with more updated annotations and H2 embedded memory DB:
application.properties
# Embedded memory database
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:h2:~/test;AUTO_SERVER=TRUE
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>com.demo</groupId>
<artifactId>jpaDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Person.java
#Entity
public class Person
{
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
private String name;
public Person() {}
public Person(String name) {this.name = name;}
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
PersonRepository.java
#Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {}
PersonService.java
#Service
public class PersonService
{
#Autowired
PersonRepository personRepository;
#Transactional
public void saveTransactional(List<Person> persons){
savePersons(persons);
}
public void saveNonTransactional(List<Person> persons){
savePersons(persons);
}
private void savePersons(List<Person> persons){
for (Person person : persons) {
if("xxx".equals(person.getName())){
throw new RuntimeException("boooom!!!");
}
personRepository.save(person);
}
}
}
Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
PersonServiceTest.java
#RunWith(SpringRunner.class)
#SpringBootTest
public class PersonServiceTest {
#Autowired
PersonService personService;
#Autowired
PersonRepository personRepository;
#Test
public void person_table_size_1() {
List<Person> persons = getPersons();
try {personService.saveNonTransactional(persons);}
catch (RuntimeException e) {}
List<Person> personsOnDB = personRepository.findAll();
assertEquals(1, personsOnDB.size());
}
#Test
public void person_table_size_0() {
List<Person> persons = getPersons();
try {personService.saveTransactional(persons);}
catch (RuntimeException e) {}
List<Person> personsOnDB = personRepository.findAll();
assertEquals(0, personsOnDB.size());
}
public List<Person> getPersons(){
List<Person> persons = new ArrayList() {{
add(new Person("aaa"));
add(new Person("xxx"));
add(new Person("sss"));
}};
return persons;
}
}
*The database is cleared and re-initialized for each test so that we always validate against a known state

Resources