BAEL-6348 persist List<String> in JPA (#13897)
* BAEL-6348 persist List<String> in JPA * Update naming
This commit is contained in:
parent
bf3b038daa
commit
6cc24e0759
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.spring.data.jpa.listrepositories.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity(name = "library")
|
||||
public class Library {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Convert(converter = StringListConverter.class)
|
||||
@Column(name = "addresses", nullable = false)
|
||||
private List<String> addresses = new ArrayList<>();
|
||||
|
||||
@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
|
||||
@CollectionTable(name = "book", joinColumns = @JoinColumn(name = "library_id"))
|
||||
@Column(name = "book", nullable = false)
|
||||
private List<String> books = new ArrayList<>();
|
||||
|
||||
public Library() {
|
||||
}
|
||||
|
||||
public Library(String name, List<String> addresses, List<String> books) {
|
||||
this.name = name;
|
||||
this.addresses = addresses;
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public void setAddresses(List<String> addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public List<String> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
public void setBooks(List<String> books) {
|
||||
this.books = books;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.spring.data.jpa.listrepositories.entity;
|
||||
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
import jakarta.persistence.Converter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import static java.util.Collections.*;
|
||||
|
||||
@Converter
|
||||
public class StringListConverter implements AttributeConverter<List<String>, String> {
|
||||
private static final String SPLIT_CHAR = ";";
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(List<String> stringList) {
|
||||
return stringList != null ? String.join(SPLIT_CHAR, stringList) : "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> convertToEntityAttribute(String string) {
|
||||
return string != null ? Arrays.asList(string.split(SPLIT_CHAR)) : emptyList();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.spring.data.jpa.listrepositories.repository;
|
||||
|
||||
import com.baeldung.spring.data.jpa.listrepositories.entity.Library;
|
||||
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;
|
||||
|
||||
@Repository
|
||||
public interface LibraryRepository extends CrudRepository<Library, Long> {
|
||||
@Query("SELECT l FROM library l JOIN FETCH l.books WHERE l.id = (:id)")
|
||||
Library findById(@Param("id") long id);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.spring.data.jpa.listrepositories.repository;
|
||||
|
||||
import com.baeldung.spring.data.jpa.listrepositories.entity.Library;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@SpringBootTest
|
||||
public class LibraryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private LibraryRepository libraryRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenLibrary_whenGetAddressesAndGetBooks_thenGetListOfItems(){
|
||||
Library library = new Library();
|
||||
library.setAddresses(Arrays.asList("Address 1", "Address 2"));
|
||||
library.setBooks(Arrays.asList("Book 1", "Book 2"));
|
||||
|
||||
libraryRepository.save(library);
|
||||
Library lib = libraryRepository.findById(library.getId().longValue());
|
||||
|
||||
Assertions.assertEquals(2, lib.getAddresses().size());
|
||||
Assertions.assertEquals(2, lib.getBooks().size());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue