BAEL-3770 JPA Entity Lifecycle Events (#8590)
This commit is contained in:
parent
caecfaed5c
commit
fe41d7d785
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package lifecycleevents;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.lifecycleevents.SpringBootLifecycleEventApplication;
|
||||||
|
import com.baeldung.lifecycleevents.model.User;
|
||||||
|
import com.baeldung.lifecycleevents.repository.UserRepository;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = SpringBootLifecycleEventApplication.class)
|
||||||
|
public class UserRepositoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
User user = new User();
|
||||||
|
user.setFirstName("Jane");
|
||||||
|
user.setLastName("Smith");
|
||||||
|
user.setUserName("jsmith123");
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void cleanup() {
|
||||||
|
userRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNewUserProvided_userIsAdded() {
|
||||||
|
User user = new User();
|
||||||
|
user.setFirstName("John");
|
||||||
|
user.setLastName("Doe");
|
||||||
|
user.setUserName("jdoe123");
|
||||||
|
user = userRepository.save(user);
|
||||||
|
assertTrue(user.getId() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUserNameProvided_userIsLoaded() {
|
||||||
|
User user = userRepository.findByUserName("jsmith123");
|
||||||
|
assertNotNull(user);
|
||||||
|
assertEquals("jsmith123", user.getUserName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExistingUserProvided_userIsUpdated() {
|
||||||
|
User user = userRepository.findByUserName("jsmith123");
|
||||||
|
user.setFirstName("Joe");
|
||||||
|
user = userRepository.save(user);
|
||||||
|
assertEquals("Joe", user.getFirstName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExistingUserDeleted_userIsDeleted() {
|
||||||
|
User user = userRepository.findByUserName("jsmith123");
|
||||||
|
userRepository.delete(user);
|
||||||
|
user = userRepository.findByUserName("jsmith123");
|
||||||
|
assertNull(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenExistingUserLoaded_fullNameIsAvailable() {
|
||||||
|
String expectedFullName = "Jane Smith";
|
||||||
|
User user = userRepository.findByUserName("jsmith123");
|
||||||
|
assertEquals(expectedFullName, user.getFullName());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user