diff --git a/persistence-modules/spring-data-jpa-repo-3/pom.xml b/persistence-modules/spring-data-jpa-repo-3/pom.xml index 8cd8ca7f61..d406d25c05 100644 --- a/persistence-modules/spring-data-jpa-repo-3/pom.xml +++ b/persistence-modules/spring-data-jpa-repo-3/pom.xml @@ -24,6 +24,7 @@ h2 runtime + org.springframework.boot spring-boot-starter-test @@ -31,5 +32,4 @@ - diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/Application.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/Application.java index aec711d0d1..94e54889f6 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/Application.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/Application.java @@ -1,6 +1,7 @@ package com.baeldung.spring.data.jpa.naturalid; import com.baeldung.spring.data.jpa.naturalid.repository.NaturalIdRepositoryImpl; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/HotelRoomsService.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/HotelRoomsService.java index c4c9b4c2b6..c8cd1fada4 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/HotelRoomsService.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/HotelRoomsService.java @@ -2,8 +2,10 @@ package com.baeldung.spring.data.jpa.naturalid; import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom; + import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; + import org.hibernate.Session; import org.springframework.stereotype.Service; @@ -12,24 +14,24 @@ import java.util.Optional; @Service public class HotelRoomsService { - private final EntityManager entityManager; + private final EntityManager entityManager; - public HotelRoomsService(EntityManagerFactory entityManagerFactory) { - this.entityManager = entityManagerFactory.createEntityManager(); - } + public HotelRoomsService(EntityManagerFactory entityManagerFactory) { + this.entityManager = entityManagerFactory.createEntityManager(); + } - public Optional conferenceRoom(String name) { - Session session = entityManager.unwrap(Session.class); - return session.bySimpleNaturalId(ConferenceRoom.class) - .loadOptional(name); - } + public Optional conferenceRoom(String name) { + Session session = entityManager.unwrap(Session.class); + return session.bySimpleNaturalId(ConferenceRoom.class) + .loadOptional(name); + } - public Optional guestRoom(int roomNumber, int floor) { - Session session = entityManager.unwrap(Session.class); - return session.byNaturalId(GuestRoom.class) - .using("roomNumber", roomNumber) - .using("floor", floor) - .loadOptional(); - } + public Optional guestRoom(int roomNumber, int floor) { + Session session = entityManager.unwrap(Session.class); + return session.byNaturalId(GuestRoom.class) + .using("roomNumber", roomNumber) + .using("floor", floor) + .loadOptional(); + } } diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdRepoConfig.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdRepositoryConfig.java similarity index 89% rename from persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdRepoConfig.java rename to persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdRepositoryConfig.java index 1461d01a46..94b96ca86c 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdRepoConfig.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdRepositoryConfig.java @@ -1,10 +1,11 @@ package com.baeldung.spring.data.jpa.naturalid; import com.baeldung.spring.data.jpa.naturalid.repository.NaturalIdRepositoryImpl; + import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration @EnableJpaRepositories(repositoryBaseClass = NaturalIdRepositoryImpl.class) -public class NaturalIdRepoConfig { +public class NaturalIdRepositoryConfig { } diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/ConferenceRoom.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/ConferenceRoom.java index 5f72993aa1..6ff7e48a3d 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/ConferenceRoom.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/ConferenceRoom.java @@ -4,53 +4,46 @@ import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; + import org.hibernate.annotations.NaturalId; @Entity public class ConferenceRoom { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @NaturalId(mutable = true) - private String name; + @NaturalId + private String name; - private int capacity; + private int capacity; - public ConferenceRoom(String name, int capacity) { - this.name = name; - this.capacity = capacity; - } + public ConferenceRoom(String name, int capacity) { + this.name = name; + this.capacity = capacity; + } - protected ConferenceRoom() { - } + protected ConferenceRoom() { + } - public Long getId() { - return id; - } + public Long getId() { + return id; + } - public void setId(Long id) { - this.id = id; - } + public String getName() { + return name; + } - public String getName() { - return name; - } + public int getCapacity() { + return capacity; + } - public int getCapacity() { - return capacity; - } + public void setCapacity(int capacity) { + this.capacity = capacity; + } - public void setCapacity(int capacity) { - this.capacity = capacity; - } - - @Override - public String toString() { - return "HotelRoom{" + - "id=" + id + - ", name='" + name + '\'' + - ", capacity=" + capacity + - '}'; - } + @Override + public String toString() { + return "HotelRoom{" + "id=" + id + ", name='" + name + '\'' + ", capacity=" + capacity + '}'; + } } diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/GuestRoom.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/GuestRoom.java index e6ec6bb3dc..1219178625 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/GuestRoom.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/entity/GuestRoom.java @@ -4,70 +4,64 @@ import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; + import org.hibernate.annotations.NaturalId; @Entity public class GuestRoom { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @NaturalId - private Integer roomNumber; + @NaturalId + private Integer roomNumber; - @NaturalId - private Integer floor; + @NaturalId + private Integer floor; - private String name; - private int capacity; + private String name; + private int capacity; + public GuestRoom(int roomNumber, int floor, String name, int capacity) { + this.roomNumber = roomNumber; + this.floor = floor; + this.name = name; + this.capacity = capacity; + } - public GuestRoom(int roomNumber, int floor, String name, int capacity) { - this.roomNumber = roomNumber; - this.floor = floor; - this.name = name; - this.capacity = capacity; - } + protected GuestRoom() { + } - protected GuestRoom() { - } + public Long getId() { + return id; + } - public Long getId() { - return id; - } + public Integer getRoomNumber() { + return roomNumber; + } - public Integer getRoomNumber() { - return roomNumber; - } + public Integer getFloor() { + return floor; + } - public Integer getFloor() { - return floor; - } + public int getCapacity() { + return capacity; + } - public int getCapacity() { - return capacity; - } + public void setCapacity(int capacity) { + this.capacity = capacity; + } - public void setCapacity(int capacity) { - this.capacity = capacity; - } + public String getName() { + return name; + } - public String getName() { - return name; - } + public void setName(String name) { + this.name = name; + } - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "GuestRoom{" + - "id=" + id + - ", roomNumber=" + roomNumber + - ", floor=" + floor + - ", name=" + name + - ", capacity=" + capacity + - '}'; - } + @Override + public String toString() { + return "GuestRoom{" + "id=" + id + ", roomNumber=" + roomNumber + ", floor=" + floor + ", name=" + name + ", capacity=" + capacity + '}'; + } } diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomJpaRepo.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomJpaRepo.java deleted file mode 100644 index 0f6d2a4b18..0000000000 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomJpaRepo.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.spring.data.jpa.naturalid.repository; - -import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface ConferenceRoomJpaRepo extends JpaRepository { -} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomRepository.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomRepository.java index 5d98ca930a..d12695f40f 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomRepository.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/ConferenceRoomRepository.java @@ -1,6 +1,7 @@ package com.baeldung.spring.data.jpa.naturalid.repository; import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; + import org.springframework.stereotype.Repository; @Repository diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/GuestRoomJpaRepo.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/GuestRoomJpaRepository.java similarity index 75% rename from persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/GuestRoomJpaRepo.java rename to persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/GuestRoomJpaRepository.java index 4caf327d76..dd761d6660 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/GuestRoomJpaRepo.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/GuestRoomJpaRepository.java @@ -1,9 +1,10 @@ package com.baeldung.spring.data.jpa.naturalid.repository; import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface GuestRoomJpaRepo extends JpaRepository { +public interface GuestRoomJpaRepository extends JpaRepository { } diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepository.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepository.java index 36a71f4ffa..5fe710c0a5 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepository.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepository.java @@ -6,6 +6,6 @@ import org.springframework.data.repository.NoRepositoryBean; import java.util.Optional; @NoRepositoryBean -public interface NaturalIdRepository extends JpaRepository { - Optional naturalId(ID naturalId); +public interface NaturalIdRepository extends JpaRepository { + Optional naturalId(ID naturalId); } diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepositoryImpl.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepositoryImpl.java index 8442237ab1..2c6e62e189 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepositoryImpl.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/naturalid/repository/NaturalIdRepositoryImpl.java @@ -1,6 +1,7 @@ package com.baeldung.spring.data.jpa.naturalid.repository; import jakarta.persistence.EntityManager; + import org.hibernate.Session; import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; @@ -11,18 +12,18 @@ import java.io.Serializable; import java.util.Optional; public class NaturalIdRepositoryImpl extends SimpleJpaRepository implements NaturalIdRepository { - private final EntityManager entityManager; + private final EntityManager entityManager; - public NaturalIdRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { - super(entityInformation, entityManager); - this.entityManager = entityManager; - } + public NaturalIdRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { + super(entityInformation, entityManager); + this.entityManager = entityManager; + } - @Override - public Optional naturalId(ID naturalId) { - return entityManager.unwrap(Session.class) - .bySimpleNaturalId(this.getDomainClass()) - .loadOptional(naturalId); - } + @Override + public Optional naturalId(ID naturalId) { + return entityManager.unwrap(Session.class) + .bySimpleNaturalId(this.getDomainClass()) + .loadOptional(naturalId); + } } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdIntegrationTest.java index d9f0c68a51..96cc805082 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/naturalid/NaturalIdIntegrationTest.java @@ -1,61 +1,56 @@ package com.baeldung.spring.data.jpa.naturalid; -import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; -import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom; -import com.baeldung.spring.data.jpa.naturalid.repository.ConferenceRoomJpaRepo; -import com.baeldung.spring.data.jpa.naturalid.repository.ConferenceRoomRepository; -import com.baeldung.spring.data.jpa.naturalid.repository.GuestRoomJpaRepo; -import jakarta.transaction.Transactional; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.Rollback; +import static org.assertj.core.api.Assertions.assertThat; import java.util.Optional; -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; +import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom; +import com.baeldung.spring.data.jpa.naturalid.repository.ConferenceRoomRepository; +import com.baeldung.spring.data.jpa.naturalid.repository.GuestRoomJpaRepository; @SpringBootTest class NaturalIdIntegrationTest { - @Autowired - private HotelRoomsService service; + @Autowired + private HotelRoomsService service; - @Autowired - private GuestRoomJpaRepo guestRoomJpaRepo; - @Autowired - private ConferenceRoomJpaRepo conferenceRoomJpaRepo; + @Autowired + private GuestRoomJpaRepository guestRoomJpaRepository; + @Autowired + private ConferenceRoomRepository conferenceRoomRepository; - @Autowired - private ConferenceRoomRepository conferenceRoomRepository; + @Test + void whenWeFindByNaturalKey_thenEntityIsReturnedCorrectly() { + guestRoomJpaRepository.save(new GuestRoom(23, 3, "B-423", 4)); - @Test - void whenWeFindByNaturalKey_thenEntityIsReturnedCorrectly() { - guestRoomJpaRepo.save(new GuestRoom(23, 3, "B-423", 4)); + Optional result = service.guestRoom(23, 3); - Optional result = service.guestRoom(23, 3); + assertThat(result).isPresent() + .hasValueSatisfying(room -> "B-423".equals(room.getName())); + } - assertThat(result).isPresent() - .hasValueSatisfying(room -> "B-423".equals(room.getName())); - } + @Test + void whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() { + conferenceRoomRepository.save(new ConferenceRoom("Colorado", 100)); - @Test - void whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() { - conferenceRoomJpaRepo.save(new ConferenceRoom("Colorado", 100)); + Optional result = service.conferenceRoom("Colorado"); - Optional result = service.conferenceRoom("Colorado"); + assertThat(result).isPresent() + .hasValueSatisfying(room -> "Colorado".equals(room.getName())); + } - assertThat(result).isPresent() - .hasValueSatisfying(room -> "Colorado".equals(room.getName())); - } + @Test + void givenNaturalIdRepository_whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() { + conferenceRoomRepository.save(new ConferenceRoom("Nevada", 200)); - @Test - void givenNaturalIdRepo_whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() { - conferenceRoomJpaRepo.save(new ConferenceRoom("Nevada", 200)); + Optional result = conferenceRoomRepository.naturalId("Nevada"); - Optional result = conferenceRoomRepository.naturalId("Nevada"); - - assertThat(result).isPresent() - .hasValueSatisfying(room -> "Nevada".equals(room.getName())); - } + assertThat(result).isPresent() + .hasValueSatisfying(room -> "Nevada".equals(room.getName())); + } } \ No newline at end of file