BAEL-6320: format & code review
This commit is contained in:
parent
7be3725480
commit
4f8662aa01
@ -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>
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ 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
|
||||||
@ -12,7 +13,7 @@ public class ConferenceRoom {
|
|||||||
@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;
|
||||||
@ -29,10 +30,6 @@ public class ConferenceRoom {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -47,10 +44,6 @@ public class ConferenceRoom {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "HotelRoom{" +
|
return "HotelRoom{" + "id=" + id + ", name='" + name + '\'' + ", capacity=" + capacity + '}';
|
||||||
"id=" + id +
|
|
||||||
", name='" + name + '\'' +
|
|
||||||
", capacity=" + capacity +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ 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
|
||||||
@ -21,7 +22,6 @@ public class GuestRoom {
|
|||||||
private String name;
|
private String name;
|
||||||
private int capacity;
|
private int capacity;
|
||||||
|
|
||||||
|
|
||||||
public GuestRoom(int roomNumber, int floor, String name, int capacity) {
|
public GuestRoom(int roomNumber, int floor, String name, int capacity) {
|
||||||
this.roomNumber = roomNumber;
|
this.roomNumber = roomNumber;
|
||||||
this.floor = floor;
|
this.floor = floor;
|
||||||
@ -62,12 +62,6 @@ public class GuestRoom {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "GuestRoom{" +
|
return "GuestRoom{" + "id=" + id + ", roomNumber=" + roomNumber + ", floor=" + floor + ", name=" + name + ", capacity=" + capacity + '}';
|
||||||
"id=" + id +
|
|
||||||
", roomNumber=" + roomNumber +
|
|
||||||
", floor=" + floor +
|
|
||||||
", name=" + name +
|
|
||||||
", capacity=" + capacity +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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> {
|
|
||||||
}
|
|
@ -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
|
||||||
|
@ -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> {
|
||||||
}
|
}
|
@ -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;
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
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 {
|
||||||
@ -22,16 +20,13 @@ class NaturalIdIntegrationTest {
|
|||||||
private HotelRoomsService service;
|
private HotelRoomsService service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private GuestRoomJpaRepo guestRoomJpaRepo;
|
private GuestRoomJpaRepository guestRoomJpaRepository;
|
||||||
@Autowired
|
|
||||||
private ConferenceRoomJpaRepo conferenceRoomJpaRepo;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ConferenceRoomRepository conferenceRoomRepository;
|
private ConferenceRoomRepository conferenceRoomRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenWeFindByNaturalKey_thenEntityIsReturnedCorrectly() {
|
void whenWeFindByNaturalKey_thenEntityIsReturnedCorrectly() {
|
||||||
guestRoomJpaRepo.save(new GuestRoom(23, 3, "B-423", 4));
|
guestRoomJpaRepository.save(new GuestRoom(23, 3, "B-423", 4));
|
||||||
|
|
||||||
Optional<GuestRoom> result = service.guestRoom(23, 3);
|
Optional<GuestRoom> result = service.guestRoom(23, 3);
|
||||||
|
|
||||||
@ -41,7 +36,7 @@ class NaturalIdIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
|
void whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
|
||||||
conferenceRoomJpaRepo.save(new ConferenceRoom("Colorado", 100));
|
conferenceRoomRepository.save(new ConferenceRoom("Colorado", 100));
|
||||||
|
|
||||||
Optional<ConferenceRoom> result = service.conferenceRoom("Colorado");
|
Optional<ConferenceRoom> result = service.conferenceRoom("Colorado");
|
||||||
|
|
||||||
@ -50,8 +45,8 @@ class NaturalIdIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenNaturalIdRepo_whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
|
void givenNaturalIdRepository_whenWeFindBySimpleNaturalKey_thenEntityIsReturnedCorrectly() {
|
||||||
conferenceRoomJpaRepo.save(new ConferenceRoom("Nevada", 200));
|
conferenceRoomRepository.save(new ConferenceRoom("Nevada", 200));
|
||||||
|
|
||||||
Optional<ConferenceRoom> result = conferenceRoomRepository.naturalId("Nevada");
|
Optional<ConferenceRoom> result = conferenceRoomRepository.naturalId("Nevada");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user