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.

2
  • 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 9, 2017 at 9:30
  • What operations, exactly, are you having issues with? In addition to adding the cascade operations suggested elsewhere see here: stackoverflow.com/questions/30464782/…
    – Alan Hay
    Nov 9, 2017 at 18:36

2 Answers 2

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.

2
  • Unlike the restbucks example, the questioner's model has a bi-directional relationship which, unless something has changed, is problematic for rest repositories. stackoverflow.com/questions/30464782/… How do you handle that in your example?
    – Alan Hay
    Nov 9, 2017 at 18:34
  • 1
    @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 setItems(List<MenuItem> items) that synchronize Menu and MenuItem and added two annotation @RestResource(exported = false) and @JsonIgnore to menu getter in MenuItem to avoid unnecessary Menu data in the response body.
    – Cepr0
    Nov 9, 2017 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

2
  • How is the insert is possible then
    – MaxExplode
    Nov 9, 2017 at 9:37
  • @MaxExplode check the definition of the CrudRepository ,docs.spring.io/spring-data/commons/docs/current/api/org/… , 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) Nov 9, 2017 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.