两种方法都有相同的擦除,但都没有覆盖另一种方法

时间:2017-08-03 08:53:58

标签: java spring repository

  

'保存(S)'在存储库中与'保存(S)'冲突在   ' org.springframework.data.repository.CrudRepository'两种方法都有   相同的擦除,但都没有超越另一个。

我收到了这个错误。我试图创建一个名为BookRepository的接口。该接口扩展了CrudRepository,我想在该接口中实现save方法和saveAll方法。当我实现所有方法时,除save()和saveAll()之外,所有方法都可以正常工作。

我给了你下面的课程。

BookRepository:

package repositories;

import com.ex.deneme_1.*;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

public interface BookRepository extends CrudRepository<Book, Long>{

    @Override
    default  <S extends T> S save(S entity) { // That doesn't work
        return null;
    }

    @Override
    default <S extends T> Iterable<S> saveAll(Iterable<S> entities) { 
        //That doesn't work either
        return null;
    }

    @Override
    default Optional<Book> findById(Long id) {
        return null;
    }

    @Override
    default boolean existsById(Long id) {
        return false;
    }

    @Override
    default Iterable<Book> findAll() {
        return null;
    }

    @Override
    default Iterable<Book> findAllById(Iterable<Long> ids) {
        return null;
    }

    @Override
    default long count() {
        return 0;
    }

    @Override
    default void deleteById(Long id) {

    }

    @Override
    default void delete(Book entity) {

    }

    @Override
    default void deleteAll(Iterable<? extends Book> entities) {

    }

    @Override
    default void deleteAll() {

    }
}

我的引导课程:

package Bootstrap;

import com.ex.deneme_1.*;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.*;
import repositories.*;

@Component
public class DevBootstrap implements ApplicationListener<ContextRefreshedEvent>{

private BookRepository bookRepository;
private AuthorRepository authorRepository;

public DevBootstrap(AuthorRepository authorRepository, BookRepository bookRepository)
{
    this.authorRepository = authorRepository;
    this.bookRepository = bookRepository;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    initData();
}


private void initData(){
    Author xyz = new Author("xyz","xyz");
    Book abc = new Book("Trying to learn Spring","1234","StackOverFlow");

    xyz.getBooks().add(abc);
    abc.getAuthors().add(xyz);

    authorRepository.save(xyz);
    bookRepository.save(abc);


}


}

和我的书类:

    package com.ex.deneme_1;
    import javax.annotation.Generated;
    import javax.persistence.*;
    import java.util.HashSet;
    import java.util.Set;
    import java.awt.*;


@Entity



public class Book extends java.awt.print.Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String title,isbn,publisher;
private long id;
@ManyToMany


 @JoinTable(name = "author_book", joinColumns = @JoinColumn(name="book_id"),
    inverseJoinColumns = @JoinColumn(name = "author_id"))

    private Set<Author> authors = new HashSet<>();

public Book(){

}

public Book(String title,String isbn, String publisher)
{
    this.title = title;
    this.isbn = isbn;
    this.publisher = publisher;
}

public Book(String title, String isbn, String publisher, Set<Author> author)
{
    this.title = title;
    this.isbn = isbn;
    this.publisher = publisher;
    this.authors = author;
}

public String getTitle() { return title; }
public String getIsbn() { return isbn; }
public String getPublisher() { return publisher; }
public Set<Author> getAuthors(){ return authors; }
public long getId() {
    return id;
}



public void setId(long id) {
    this.id = id;
}
public void setTitle(String title) { this.title = title; }
public void setIsbn(String isbn) { this.isbn = isbn; }
public void setPublisher(String publisher){ this.publisher = publisher; }
public void setAuthors(Set<Author> authors){ this.authors = authors; }


}

我认为你们很容易猜到我有一个名为Author的类,它具有类似Book.class的功能。

2 个答案:

答案 0 :(得分:1)

既然你正在扩展CrudRepository,那么save方法就会期待Book,这意味着保存(Book entity)

答案 1 :(得分:1)

是否有特殊原因要覆盖CrudRepository中的save方法? Spring数据的工作方式是它将自动实现CrudRepository接口的任何接口扩展,并为您提供保存实体的开箱即用方法,因此您不需要真正覆盖它。