[JAVA-2306] Fixes after Loredana's review
* Brought back UserRepositoryCustomImpl.java * Added link to https://www.baeldung.com/spring-data-jpa-query in spring-data-jpa-query-2 README * Migrated code for https://www.baeldung.com/spring-data-jpa-query-by-date * Migrated https://www.baeldung.com/spring-vs-jta-transactional to spring-persistence-simple
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
### Relevant Articles:
|
||||
- [Transaction Propagation and Isolation in Spring @Transactional](https://www.baeldung.com/spring-transactional-propagation-isolation)
|
||||
- [JTA Transaction with Spring](https://www.baeldung.com/spring-vs-jta-transactional)
|
||||
- [Mock JNDI Datasource](https://www.baeldung.com/spring-mock-jndi-datasource)
|
||||
|
||||
### Eclipse Config
|
||||
|
||||
@@ -26,6 +26,31 @@
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
<dependency>
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
<version>${persistence-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>${spring-data-jpa.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.transaction</groupId>
|
||||
<artifactId>javax.transaction-api</artifactId>
|
||||
<version>${transaction-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@@ -58,7 +83,11 @@
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.2.4.RELEASE</org.springframework.version>
|
||||
<spring-boot-starter.version>2.3.3.RELEASE</spring-boot-starter.version>
|
||||
<!-- persistence -->
|
||||
<persistence-api.version>2.2</persistence-api.version>
|
||||
<transaction-api.version>1.3</transaction-api.version>
|
||||
<spring-data-jpa.version>2.2.7.RELEASE</spring-data-jpa.version>
|
||||
<h2.version>1.4.200</h2.version>
|
||||
<!-- simple-jndi -->
|
||||
<simple-jndi.version>0.23.0</simple-jndi.version>
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.spring.transactional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Car {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String make;
|
||||
|
||||
private String model;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(Long id, String make, String model) {
|
||||
this.id = id;
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car{" +
|
||||
"id=" + id +
|
||||
", make='" + make + '\'' +
|
||||
", model='" + model + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.spring.transactional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CarRepository extends JpaRepository<Car, Long> {
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.spring.transactional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
|
||||
@Service
|
||||
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.SUPPORTS, readOnly = false, timeout = 30)
|
||||
public class CarService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Transactional(rollbackFor = IllegalArgumentException.class, noRollbackFor = EntityExistsException.class,
|
||||
rollbackForClassName = "IllegalArgumentException", noRollbackForClassName = "EntityExistsException")
|
||||
public Car save(Car car) {
|
||||
return carRepository.save(car);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.spring.transactional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional(Transactional.TxType.SUPPORTS)
|
||||
public class RentalService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
|
||||
@Transactional(rollbackOn = IllegalArgumentException.class, dontRollbackOn = EntityExistsException.class)
|
||||
public Car rent(Car car) {
|
||||
return carRepository.save(car);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user