Merge branch 'master' into master
This commit is contained in:
commit
3369cdfdc1
|
@ -30,6 +30,11 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.hypersistence</groupId>
|
||||
<artifactId>hypersistence-utils-hibernate-55</artifactId>
|
||||
<version>${hypersistance-utils-hibernate-55.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
|
@ -47,6 +52,30 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -98,4 +127,9 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<hypersistance-utils-hibernate-55.version>3.7.0</hypersistance-utils-hibernate-55.version>
|
||||
<postgresql.version>42.7.1</postgresql.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
public class Address {
|
||||
|
||||
private String postCode;
|
||||
|
||||
private String city;
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
|
||||
@Converter
|
||||
@Slf4j
|
||||
public class AddressAttributeConverter implements AttributeConverter<Address, String> {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(Address address) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(address);
|
||||
} catch (JsonProcessingException jpe) {
|
||||
log.warn("Cannot convert Address into JSON");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Address convertToEntityAttribute(String value) {
|
||||
try {
|
||||
return objectMapper.readValue(value, Address.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("Cannot convert JSON into Address");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class JsonAttributeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(JsonAttributeApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
@Entity
|
||||
@Table(name = "student")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
|
||||
public class StudentEntity {
|
||||
|
||||
@Id
|
||||
@Column(name = "student_id", length = 8)
|
||||
private String id;
|
||||
|
||||
@Column(name = "admit_year", length = 4)
|
||||
private String admitYear;
|
||||
|
||||
@Type(type = "jsonb")
|
||||
@Column(name = "address", columnDefinition = "jsonb")
|
||||
private Address address;
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface StudentRepository extends CrudRepository<StudentEntity, String> {
|
||||
|
||||
@Query(value = "SELECT * FROM student WHERE address->>'postCode' = :postCode", nativeQuery = true)
|
||||
List<StudentEntity> findByAddressPostCode(@Param("postCode") String postCode);
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "student_str")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
public class StudentStrEntity {
|
||||
|
||||
@Id
|
||||
@Column(name = "student_id", length = 8)
|
||||
private String id;
|
||||
|
||||
@Column(name = "admit_year", length = 4)
|
||||
private String admitYear;
|
||||
|
||||
@Convert(converter = AddressAttributeConverter.class)
|
||||
@Column(name = "address", length = 500)
|
||||
private Address address;
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface StudentStrRepository extends CrudRepository<StudentStrEntity, String> {
|
||||
|
||||
@Query(value = "SELECT * FROM student WHERE address->>'postCode' = :postCode", nativeQuery = true)
|
||||
List<StudentStrEntity> findByAddressPostCode(@Param("postCode") String postCode);
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.spring.data.persistence.json;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest(classes = JsonAttributeApplication.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class JsonAttributeLiveTest {
|
||||
|
||||
@Inject
|
||||
private StudentStrRepository studentStrRepository;
|
||||
|
||||
@Inject
|
||||
private StudentRepository studentRepository;
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void whenSaveAnStudentStrEntityAndFindById_thenTheRecordPresentsInDb() {
|
||||
String studentId = "23876371";
|
||||
String postCode = "KT6 7BB";
|
||||
|
||||
Address address = new Address(postCode, "London");
|
||||
StudentStrEntity studentStrEntity = StudentStrEntity.builder()
|
||||
.id(studentId)
|
||||
.admitYear("2023")
|
||||
.address(address)
|
||||
.build();
|
||||
|
||||
StudentStrEntity savedStudentStrEntity = studentStrRepository.save(studentStrEntity);
|
||||
|
||||
Optional<StudentStrEntity> studentEntityOptional = studentStrRepository.findById(studentId);
|
||||
assertThat(studentEntityOptional.isPresent()).isTrue();
|
||||
|
||||
studentStrEntity = studentEntityOptional.get();
|
||||
assertThat(studentStrEntity.getId()).isEqualTo(studentId);
|
||||
assertThat(studentStrEntity.getAddress().getPostCode()).isEqualTo(postCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void whenSaveAnStudentEntityAndFindById_thenTheRecordPresentsInDb() {
|
||||
String studentId = "23876371";
|
||||
String postCode = "KT6 7BB";
|
||||
|
||||
Address address = new Address(postCode, "London");
|
||||
StudentEntity studentEntity = StudentEntity.builder()
|
||||
.id(studentId)
|
||||
.admitYear("2023")
|
||||
.address(address)
|
||||
.build();
|
||||
|
||||
StudentEntity savedStudentEntity = studentRepository.save(studentEntity);
|
||||
|
||||
Optional<StudentEntity> studentEntityOptional = studentRepository.findById(studentId);
|
||||
assertThat(studentEntityOptional.isPresent()).isTrue();
|
||||
|
||||
studentEntity = studentEntityOptional.get();
|
||||
assertThat(studentEntity.getId()).isEqualTo(studentId);
|
||||
assertThat(studentEntity.getAddress().getPostCode()).isEqualTo(postCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(50)
|
||||
void whenFindByAddressPostCode_thenReturnListIsNotEmpty() {
|
||||
String postCode = "KT6 7BB";
|
||||
List<StudentStrEntity> studentStrEntityList = studentStrRepository.findByAddressPostCode(postCode);
|
||||
assertThat(studentStrEntityList).isNotEmpty();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue