JAVA-66: New module spring-data-jpa-annotations
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
## Spring Data JPA - Annotations
|
||||
|
||||
This module contains articles about annotations used in Spring Data JPA
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [Spring Data Annotations](https://www.baeldung.com/spring-data-annotations)
|
||||
- [DDD Aggregates and @DomainEvents](https://www.baeldung.com/spring-data-ddd)
|
||||
- [JPA @Embedded And @Embeddable](https://www.baeldung.com/jpa-embedded-embeddable)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
- [Spring JPA @Embedded and @EmbeddedId](https://www.baeldung.com/spring-jpa-embedded-method-parameters)
|
||||
- [Programmatic Transaction Management in Spring](https://www.baeldung.com/spring-programmatic-transaction-management)
|
||||
- [JPA Entity Lifecycle Events](https://www.baeldung.com/jpa-entity-lifecycle-events)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
"No persistence xml file found in project"
|
||||
|
||||
This can be ignored:
|
||||
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
|
||||
Or:
|
||||
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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>
|
||||
<artifactId>spring-data-jpa-annotations</artifactId>
|
||||
<name>spring-data-jpa-annotations</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-ehcache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-envers</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Test containers only dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${testcontainers.postgresql.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
<!-- Test containers only dependencies -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</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>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.boot.Application</start-class>
|
||||
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
|
||||
<postgresql.version>42.2.5</postgresql.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
@Entity
|
||||
class Aggregate {
|
||||
@Transient
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private Aggregate() {
|
||||
}
|
||||
|
||||
Aggregate(long id, ApplicationEventPublisher eventPublisher) {
|
||||
this.id = id;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DomainEntity [id=" + id + "]";
|
||||
}
|
||||
|
||||
void domainOperation() {
|
||||
// some business logic
|
||||
if (eventPublisher != null) {
|
||||
eventPublisher.publishEvent(new DomainEvent());
|
||||
}
|
||||
}
|
||||
|
||||
long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.springframework.data.domain.AfterDomainEventPublication;
|
||||
import org.springframework.data.domain.DomainEvents;
|
||||
|
||||
@Entity
|
||||
public class Aggregate2 {
|
||||
@Transient
|
||||
private final Collection<DomainEvent> domainEvents;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
public Aggregate2() {
|
||||
domainEvents = new ArrayList<>();
|
||||
}
|
||||
|
||||
@AfterDomainEventPublication
|
||||
public void clearEvents() {
|
||||
domainEvents.clear();
|
||||
}
|
||||
|
||||
public void domainOperation() {
|
||||
// some domain operation
|
||||
domainEvents.add(new DomainEvent());
|
||||
}
|
||||
|
||||
@DomainEvents
|
||||
public Collection<DomainEvent> events() {
|
||||
return domainEvents;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface Aggregate2Repository extends CrudRepository<Aggregate2, Long> {
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.springframework.data.domain.AbstractAggregateRoot;
|
||||
|
||||
@Entity
|
||||
public class Aggregate3 extends AbstractAggregateRoot<Aggregate3> {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
public void domainOperation() {
|
||||
// some domain operation
|
||||
registerEvent(new DomainEvent());
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* @author goobar
|
||||
*
|
||||
*/
|
||||
public interface Aggregate3Repository extends CrudRepository<Aggregate3, Long> {
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface AggregateRepository extends CrudRepository<Aggregate, Long> {
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
@PropertySource("classpath:/ddd.properties")
|
||||
public class DddConfig {
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
class DomainEvent {
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DomainService {
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
private final AggregateRepository repository;
|
||||
|
||||
public DomainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) {
|
||||
this.repository = repository;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void serviceDomainOperation(long entityId) {
|
||||
repository.findById(entityId)
|
||||
.ifPresent(entity -> {
|
||||
entity.domainOperation();
|
||||
repository.save(entity);
|
||||
eventPublisher.publishEvent(new DomainEvent());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BookApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BookApplication.class);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.composite.entity;
|
||||
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@EmbeddedId
|
||||
private BookId id;
|
||||
private String genre;
|
||||
private Integer price;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String author, String name, String genre, Integer price) {
|
||||
BookId id = new BookId(author, name);
|
||||
this.id = id;
|
||||
this.genre = genre;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public BookId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(BookId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getGenre() {
|
||||
return genre;
|
||||
}
|
||||
|
||||
public void setGenre(String genre) {
|
||||
this.genre = genre;
|
||||
}
|
||||
|
||||
public Integer getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Integer price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.composite.entity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
@Embeddable
|
||||
public class BookId implements Serializable {
|
||||
|
||||
private String author;
|
||||
private String name;
|
||||
|
||||
public BookId() {
|
||||
}
|
||||
|
||||
public BookId(String author, String name) {
|
||||
this.author = author;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
BookId bookId = (BookId) o;
|
||||
return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(author, name);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.composite.repository;
|
||||
|
||||
import com.baeldung.composite.entity.Book;
|
||||
import com.baeldung.composite.entity.BookId;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends JpaRepository<Book, BookId> {
|
||||
|
||||
List<Book> findByIdName(String name);
|
||||
|
||||
List<Book> findByIdAuthor(String author);
|
||||
|
||||
List<Book> findByGenre(String genre);
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.embeddable.model;
|
||||
|
||||
import javax.persistence.AttributeOverride;
|
||||
import javax.persistence.AttributeOverrides;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embedded;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Company {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String address;
|
||||
|
||||
private String phone;
|
||||
|
||||
@Embedded
|
||||
@AttributeOverrides(value = {
|
||||
@AttributeOverride( name = "firstName", column = @Column(name = "contact_first_name")),
|
||||
@AttributeOverride( name = "lastName", column = @Column(name = "contact_last_name")),
|
||||
@AttributeOverride( name = "phone", column = @Column(name = "contact_phone"))
|
||||
})
|
||||
private ContactPerson contactPerson;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public ContactPerson getContactPerson() {
|
||||
return contactPerson;
|
||||
}
|
||||
|
||||
public void setContactPerson(ContactPerson contactPerson) {
|
||||
this.contactPerson = contactPerson;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.embeddable.model;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class ContactPerson {
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String phone;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.embeddable.repositories;
|
||||
|
||||
import com.baeldung.embeddable.model.Company;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CompanyRepository extends JpaRepository<Company, Integer> {
|
||||
|
||||
List<Company> findByContactPersonFirstName(String firstName);
|
||||
|
||||
@Query("SELECT C FROM Company C WHERE C.contactPerson.firstName = ?1")
|
||||
List<Company> findByContactPersonFirstNameWithJPQL(String firstName);
|
||||
|
||||
@Query(value = "SELECT * FROM company WHERE contact_first_name = ?1", nativeQuery = true)
|
||||
List<Company> findByContactPersonFirstNameWithNativeQuery(String firstName);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.lifecycleevents;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootLifecycleEventApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootLifecycleEventApplication.class, args);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.lifecycleevents.model;
|
||||
|
||||
import javax.persistence.PostLoad;
|
||||
import javax.persistence.PostPersist;
|
||||
import javax.persistence.PostRemove;
|
||||
import javax.persistence.PostUpdate;
|
||||
import javax.persistence.PrePersist;
|
||||
import javax.persistence.PreRemove;
|
||||
import javax.persistence.PreUpdate;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class AuditTrailListener {
|
||||
private static Log log = LogFactory.getLog(AuditTrailListener.class);
|
||||
|
||||
@PrePersist
|
||||
@PreUpdate
|
||||
@PreRemove
|
||||
private void beforeAnyUpdate(User user) {
|
||||
if (user.getId() == 0) {
|
||||
log.info("[USER AUDIT] About to add a user");
|
||||
} else {
|
||||
log.info("[USER AUDIT] About to update/delete user: " + user.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@PostPersist
|
||||
@PostUpdate
|
||||
@PostRemove
|
||||
private void afterAnyUpdate(User user) {
|
||||
log.info("[USER AUDIT] add/update/delete complete for user: " + user.getId());
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
private void afterLoad(User user) {
|
||||
log.info("[USER AUDIT] user loaded from database: " + user.getId());
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package com.baeldung.lifecycleevents.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EntityListeners;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.PostLoad;
|
||||
import javax.persistence.PostPersist;
|
||||
import javax.persistence.PostRemove;
|
||||
import javax.persistence.PostUpdate;
|
||||
import javax.persistence.PrePersist;
|
||||
import javax.persistence.PreRemove;
|
||||
import javax.persistence.PreUpdate;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@Entity
|
||||
@EntityListeners(AuditTrailListener.class)
|
||||
public class User {
|
||||
private static Log log = LogFactory.getLog(User.class);
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
private String userName;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
@Transient
|
||||
private String fullName;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
@PrePersist
|
||||
public void logNewUserAttempt() {
|
||||
log.info("Attempting to add new user with username: " + userName);
|
||||
}
|
||||
|
||||
@PostPersist
|
||||
public void logNewUserAdded() {
|
||||
log.info("Added user '" + userName + "' with ID: " + id);
|
||||
}
|
||||
|
||||
@PreRemove
|
||||
public void logUserRemovalAttempt() {
|
||||
log.info("Attempting to delete user: " + userName);
|
||||
}
|
||||
|
||||
@PostRemove
|
||||
public void logUserRemoval() {
|
||||
log.info("Deleted user: " + userName);
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
public void logUserUpdateAttempt() {
|
||||
log.info("Attempting to update user: " + userName);
|
||||
}
|
||||
|
||||
@PostUpdate
|
||||
public void logUserUpdate() {
|
||||
log.info("Updated user: " + userName);
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
public void logUserLoad() {
|
||||
fullName = firstName + " " + lastName;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.lifecycleevents.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.lifecycleevents.model.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
public User findByUserName(String userName);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.tx;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TxApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TxApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.tx.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Payment {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private Long amount;
|
||||
|
||||
@Column(unique = true)
|
||||
private String referenceNumber;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private State state;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(Long amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getReferenceNumber() {
|
||||
return referenceNumber;
|
||||
}
|
||||
|
||||
public void setReferenceNumber(String referenceNumber) {
|
||||
this.referenceNumber = referenceNumber;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public enum State {
|
||||
STARTED, FAILED, SUCCESSFUL
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
spring.jpa.properties.hibernate.jdbc.batch_size=4
|
||||
spring.jpa.properties.hibernate.order_inserts=true
|
||||
spring.jpa.properties.hibernate.order_updates=true
|
||||
spring.jpa.properties.hibernate.generate_statistics=true
|
||||
@@ -0,0 +1 @@
|
||||
spring.datasource.initialization-mode=never
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
# jdbc.X
|
||||
jdbc.driverClassName=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
|
||||
jdbc.user=sa
|
||||
jdbc.pass=sa
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.cache.use_second_level_cache=true
|
||||
hibernate.cache.use_query_cache=true
|
||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import com.baeldung.boot.ddd.event.Aggregate2;
|
||||
import com.baeldung.boot.ddd.event.Aggregate2Repository;
|
||||
import com.baeldung.boot.ddd.event.DomainEvent;
|
||||
|
||||
@SpringJUnitConfig
|
||||
@SpringBootTest
|
||||
class Aggregate2EventsIntegrationTest {
|
||||
@MockBean
|
||||
private TestEventHandler eventHandler;
|
||||
@Autowired
|
||||
private Aggregate2Repository repository;
|
||||
|
||||
// @formatter:off
|
||||
@DisplayName("given aggregate with @AfterDomainEventPublication,"
|
||||
+ " when do domain operation and save twice,"
|
||||
+ " then an event is published only for the first time")
|
||||
// @formatter:on
|
||||
@Test
|
||||
void afterDomainEvents() {
|
||||
// given
|
||||
Aggregate2 aggregate = new Aggregate2();
|
||||
|
||||
// when
|
||||
aggregate.domainOperation();
|
||||
repository.save(aggregate);
|
||||
repository.save(aggregate);
|
||||
|
||||
// then
|
||||
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
// @formatter:off
|
||||
@DisplayName("given aggregate with @DomainEvents,"
|
||||
+ " when do domain operation and save,"
|
||||
+ " then an event is published")
|
||||
// @formatter:on
|
||||
@Test
|
||||
void domainEvents() {
|
||||
// given
|
||||
Aggregate2 aggregate = new Aggregate2();
|
||||
|
||||
// when
|
||||
aggregate.domainOperation();
|
||||
repository.save(aggregate);
|
||||
|
||||
// then
|
||||
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user