1

I need to know is there any possibility to manage several entities with one crud repository in spring data rest.

Example :

Library entity

@Entity
public class Library {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String name;

    @OneToMany(mappedBy = "library")
    private List<Book> books;
}

Book entity

@Entity
public class Book {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable=false)
    private String title;

    @ManyToOne
    @JoinColumn(name="library_id")
    private Library library;

}

My requirement is

public interface LibraryRepository extends CrudRepository<Library, Long> { }

is to have only this repository to manage both library and the book entities.

I tried inserting and it is working well so far. but other operations are not supported by this approach. is there any other approach rather than having two crud repositories to do this.

MaxExplode
  • 1,977
  • 2
  • 17
  • 27
  • You cannot. You need to create separate Repository interfaces for each entity since each specific repository is responsible for converting its database data into its corresponding java object. – Hari Haran Nov 09 '17 at 09:30
  • What operations, exactly, are you having issues with? In addition to adding the cascade operations suggested elsewhere see here: https://stackoverflow.com/questions/30464782/how-to-maintain-bi-directional-relationships-with-spring-data-rest-and-jpa – Alan Hay Nov 09 '17 at 18:36

2 Answers2

4

Of course you can. Just correct a little your Library like this:

@OneToMany(mappedBy = "library", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Book> books;

Then you can create/update your Library and its books with this payload:

{
    "name": "library1",
    "books": [
        {
            "title": "book1"
        },
        {
            "title": "book2"
        }
    ]
}

Code example of the Spring Data author.

My example.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • Unlike the restbucks example, the questioner's model has a bi-directional relationship which, unless something has changed, is problematic for rest repositories. https://stackoverflow.com/questions/30464782/how-to-maintain-bi-directional-relationships-with-spring-data-rest-and-jpa How do you handle that in your example? – Alan Hay Nov 09 '17 at 18:34
  • @AlanHay I agree that the bi-di relation here is not needed. And I know about the problem you pointed. In my example I [implemented](https://github.com/Cepr0/restvotes/blob/master/src/main/java/restvotes/domain/entity/Menu.java) `setItems(List items)` that synchronize `Menu` and `MenuItem` and [added](https://github.com/Cepr0/restvotes/blob/master/src/main/java/restvotes/domain/entity/MenuItem.java) two annotation `@RestResource(exported = false)` and `@JsonIgnore` to menu getter in `MenuItem` to avoid unnecessary `Menu` data in the response body. – Cepr0 Nov 09 '17 at 20:23
0

You cannot do that simply, since a bean will be created for each repository and this bean should be instantiated with entity type defined

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • How is the insert is possible then – MaxExplode Nov 09 '17 at 09:37
  • @MaxExplode check the definition of the CrudRepository ,https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html , its shipped with two Generics , so when instantiating it you need to specify these generics and when creating the bean the defined type will be shipped with the bean , so save methos is save( S entity) where S is the entity so when you define the CrudRepository with entity X for example, save will be defined as save( X entity) – Amer Qarabsa Nov 09 '17 at 09:42