BAEL-6320: format & code review

This commit is contained in:
emanueltrandafir1993 2023-05-14 16:52:59 +02:00
parent 7be3725480
commit 4f8662aa01
12 changed files with 147 additions and 167 deletions

View File

@ -24,6 +24,7 @@
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
@ -31,5 +32,4 @@
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,6 +1,7 @@
package com.baeldung.spring.data.jpa.naturalid; package com.baeldung.spring.data.jpa.naturalid;
import com.baeldung.spring.data.jpa.naturalid.repository.NaturalIdRepositoryImpl; import com.baeldung.spring.data.jpa.naturalid.repository.NaturalIdRepositoryImpl;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

View File

@ -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.ConferenceRoom;
import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom; import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityManagerFactory;
import org.hibernate.Session; import org.hibernate.Session;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,24 +14,24 @@ import java.util.Optional;
@Service @Service
public class HotelRoomsService { public class HotelRoomsService {
private final EntityManager entityManager; private final EntityManager entityManager;
public HotelRoomsService(EntityManagerFactory entityManagerFactory) { public HotelRoomsService(EntityManagerFactory entityManagerFactory) {
this.entityManager = entityManagerFactory.createEntityManager(); this.entityManager = entityManagerFactory.createEntityManager();
} }
public Optional<ConferenceRoom> conferenceRoom(String name) { public Optional<ConferenceRoom> conferenceRoom(String name) {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
return session.bySimpleNaturalId(ConferenceRoom.class) return session.bySimpleNaturalId(ConferenceRoom.class)
.loadOptional(name); .loadOptional(name);
} }
public Optional<GuestRoom> guestRoom(int roomNumber, int floor) { public Optional<GuestRoom> guestRoom(int roomNumber, int floor) {
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
return session.byNaturalId(GuestRoom.class) return session.byNaturalId(GuestRoom.class)
.using("roomNumber", roomNumber) .using("roomNumber", roomNumber)
.using("floor", floor) .using("floor", floor)
.loadOptional(); .loadOptional();
} }
} }

View File

@ -1,10 +1,11 @@
package com.baeldung.spring.data.jpa.naturalid; package com.baeldung.spring.data.jpa.naturalid;
import com.baeldung.spring.data.jpa.naturalid.repository.NaturalIdRepositoryImpl; import com.baeldung.spring.data.jpa.naturalid.repository.NaturalIdRepositoryImpl;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration @Configuration
@EnableJpaRepositories(repositoryBaseClass = NaturalIdRepositoryImpl.class) @EnableJpaRepositories(repositoryBaseClass = NaturalIdRepositoryImpl.class)
public class NaturalIdRepoConfig { public class NaturalIdRepositoryConfig {
} }

View File

@ -4,53 +4,46 @@ import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
@Entity @Entity
public class ConferenceRoom { public class ConferenceRoom {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@NaturalId(mutable = true) @NaturalId
private String name; private String name;
private int capacity; private int capacity;
public ConferenceRoom(String name, int capacity) { public ConferenceRoom(String name, int capacity) {
this.name = name; this.name = name;
this.capacity = capacity; this.capacity = capacity;
} }
protected ConferenceRoom() { protected ConferenceRoom() {
} }
public Long getId() { public Long getId() {
return id; return id;
} }
public void setId(Long id) { public String getName() {
this.id = id; return name;
} }
public String getName() { public int getCapacity() {
return name; return capacity;
} }
public int getCapacity() { public void setCapacity(int capacity) {
return capacity; this.capacity = capacity;
} }
public void setCapacity(int capacity) { @Override
this.capacity = capacity; public String toString() {
} return "HotelRoom{" + "id=" + id + ", name='" + name + '\'' + ", capacity=" + capacity + '}';
}
@Override
public String toString() {
return "HotelRoom{" +
"id=" + id +
", name='" + name + '\'' +
", capacity=" + capacity +
'}';
}
} }

View File

@ -4,70 +4,64 @@ import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
@Entity @Entity
public class GuestRoom { public class GuestRoom {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@NaturalId @NaturalId
private Integer roomNumber; private Integer roomNumber;
@NaturalId @NaturalId
private Integer floor; private Integer floor;
private String name; private String name;
private int capacity; 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) { protected GuestRoom() {
this.roomNumber = roomNumber; }
this.floor = floor;
this.name = name;
this.capacity = capacity;
}
protected GuestRoom() { public Long getId() {
} return id;
}
public Long getId() { public Integer getRoomNumber() {
return id; return roomNumber;
} }
public Integer getRoomNumber() { public Integer getFloor() {
return roomNumber; return floor;
} }
public Integer getFloor() { public int getCapacity() {
return floor; return capacity;
} }
public int getCapacity() { public void setCapacity(int capacity) {
return capacity; this.capacity = capacity;
} }
public void setCapacity(int capacity) { public String getName() {
this.capacity = capacity; return name;
} }
public String getName() { public void setName(String name) {
return name; this.name = name;
} }
public void setName(String name) { @Override
this.name = name; 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 +
'}';
}
} }

View File

@ -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<ConferenceRoom, Long> {
}

View File

@ -1,6 +1,7 @@
package com.baeldung.spring.data.jpa.naturalid.repository; package com.baeldung.spring.data.jpa.naturalid.repository;
import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository

View File

@ -1,9 +1,10 @@
package com.baeldung.spring.data.jpa.naturalid.repository; package com.baeldung.spring.data.jpa.naturalid.repository;
import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom; import com.baeldung.spring.data.jpa.naturalid.entity.GuestRoom;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface GuestRoomJpaRepo extends JpaRepository<GuestRoom, Long> { public interface GuestRoomJpaRepository extends JpaRepository<GuestRoom, Long> {
} }

View File

@ -6,6 +6,6 @@ import org.springframework.data.repository.NoRepositoryBean;
import java.util.Optional; import java.util.Optional;
@NoRepositoryBean @NoRepositoryBean
public interface NaturalIdRepository<T, ID> extends JpaRepository<T, ID> { public interface NaturalIdRepository<T, ID> extends JpaRepository<T, ID> {
Optional<T> naturalId(ID naturalId); Optional<T> naturalId(ID naturalId);
} }

View File

@ -1,6 +1,7 @@
package com.baeldung.spring.data.jpa.naturalid.repository; package com.baeldung.spring.data.jpa.naturalid.repository;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import org.hibernate.Session; import org.hibernate.Session;
import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
@ -11,18 +12,18 @@ import java.io.Serializable;
import java.util.Optional; import java.util.Optional;
public class NaturalIdRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements NaturalIdRepository<T, ID> { public class NaturalIdRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements NaturalIdRepository<T, ID> {
private final EntityManager entityManager; private final EntityManager entityManager;
public NaturalIdRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) { public NaturalIdRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager); super(entityInformation, entityManager);
this.entityManager = entityManager; this.entityManager = entityManager;
} }
@Override @Override
public Optional<T> naturalId(ID naturalId) { public Optional<T> naturalId(ID naturalId) {
return entityManager.unwrap(Session.class) return entityManager.unwrap(Session.class)
.bySimpleNaturalId(this.getDomainClass()) .bySimpleNaturalId(this.getDomainClass())
.loadOptional(naturalId); .loadOptional(naturalId);
} }
} }

View File

@ -1,61 +1,56 @@
package com.baeldung.spring.data.jpa.naturalid; package com.baeldung.spring.data.jpa.naturalid;
import com.baeldung.spring.data.jpa.naturalid.entity.ConferenceRoom; import static org.assertj.core.api.Assertions.assertThat;
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 java.util.Optional; 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 @SpringBootTest
class NaturalIdIntegrationTest { class NaturalIdIntegrationTest {
@Autowired @Autowired
private HotelRoomsService service; private HotelRoomsService service;
@Autowired @Autowired
private GuestRoomJpaRepo guestRoomJpaRepo; private GuestRoomJpaRepository guestRoomJpaRepository;
@Autowired @Autowired
private ConferenceRoomJpaRepo conferenceRoomJpaRepo; private ConferenceRoomRepository conferenceRoomRepository;
@Autowired @Test
private ConferenceRoomRepository conferenceRoomRepository; void whenWeFindByNaturalKey_thenEntityIsReturnedCorrectly() {
guestRoomJpaRepository.save(new GuestRoom(23, 3, "B-423", 4));
@Test Optional<GuestRoom> result = service.guestRoom(23, 3);
void whenWeFindByNaturalKey_thenEntityIsReturnedCorrectly() {
guestRoomJpaRepo.save(new GuestRoom(23, 3, "B-423", 4));
Optional<GuestRoom> result = service.guestRoom(23, 3); assertThat(result).isPresent()
.hasValueSatisfying(room -> "B-423".equals(room.getName()));
}
assertThat(result).isPresent() @Test
.hasValueSatisfying(room -> "B-423".equals(room.getName())); void whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
} conferenceRoomRepository.save(new ConferenceRoom("Colorado", 100));
@Test Optional<ConferenceRoom> result = service.conferenceRoom("Colorado");
void whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
conferenceRoomJpaRepo.save(new ConferenceRoom("Colorado", 100));
Optional<ConferenceRoom> result = service.conferenceRoom("Colorado"); assertThat(result).isPresent()
.hasValueSatisfying(room -> "Colorado".equals(room.getName()));
}
assertThat(result).isPresent() @Test
.hasValueSatisfying(room -> "Colorado".equals(room.getName())); void givenNaturalIdRepository_whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
} conferenceRoomRepository.save(new ConferenceRoom("Nevada", 200));
@Test Optional<ConferenceRoom> result = conferenceRoomRepository.naturalId("Nevada");
void givenNaturalIdRepo_whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
conferenceRoomJpaRepo.save(new ConferenceRoom("Nevada", 200));
Optional<ConferenceRoom> result = conferenceRoomRepository.naturalId("Nevada"); assertThat(result).isPresent()
.hasValueSatisfying(room -> "Nevada".equals(room.getName()));
assertThat(result).isPresent() }
.hasValueSatisfying(room -> "Nevada".equals(room.getName()));
}
} }