spring data rest projections
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
|
||||
|
||||
import com.baeldung.projections.CustomBook;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class RestConfig extends RepositoryRestConfigurerAdapter{
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration){
|
||||
repositoryRestConfiguration.getProjectionConfiguration().addProjection(CustomBook.class);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
@@ -20,12 +21,14 @@ public class Book {
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
|
||||
private String isbn;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "library_id")
|
||||
private Library library;
|
||||
|
||||
@ManyToMany(mappedBy = "books")
|
||||
@ManyToMany(mappedBy = "books", fetch = FetchType.EAGER)
|
||||
private List<Author> authors;
|
||||
|
||||
public Book() {
|
||||
@@ -52,6 +55,15 @@ public class Book {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
|
||||
public Library getLibrary() {
|
||||
return library;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.projections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.rest.core.config.Projection;
|
||||
|
||||
import com.baeldung.models.Author;
|
||||
import com.baeldung.models.Book;
|
||||
|
||||
@Projection(name = "customBook", types = { Book.class })
|
||||
public interface CustomBook {
|
||||
@Value("#{target.id}")
|
||||
long getId();
|
||||
|
||||
String getTitle();
|
||||
|
||||
List<Author> getAuthors();
|
||||
|
||||
@Value("#{target.getAuthors().size()}")
|
||||
int getAuthorCount();
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.baeldung.repositories;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||
|
||||
import com.baeldung.models.Book;
|
||||
import com.baeldung.projections.CustomBook;
|
||||
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||
|
||||
}
|
||||
@RepositoryRestResource(excerptProjection = CustomBook.class)
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {}
|
||||
|
||||
Reference in New Issue
Block a user