[英]Both methods have same erasure, yet neither overrides the other
'save(S)' in repositories clashes with 'save(S)' in 'org.springframework.data.repository.CrudRepository' both methods have same erasure, yet neither overrides the other. 存储库中的“ save(S)”与“ org.springframework.data.repository.CrudRepository”中的“ save(S)”相冲突,但是这两种方法都具有相同的擦除方式,但是两者都不会覆盖其他方法。
I got this error. 我得到这个错误。 I'm trying to create an interface which is named as BookRepository. 我正在尝试创建一个名为BookRepository的接口。 The interface extends CrudRepository and I want to implement save method and saveAll method inside that interface. 该接口扩展了CrudRepository,我想在该接口内实现save方法和saveAll方法。 When I implement all methods, all methods work fine except save() and saveAll(). 当我实现所有方法时,除了save()和saveAll()之外,所有方法都可以正常工作。
I gave you my classes below. 我在下面给你我的课。
BookRepository: 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() {
}
}
My bootstrap class: 我的引导程序类:
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);
}
}
And My Book Class: 和我的书类:
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; }
}
I think you guys easily guessed I have a class named as Author which has similar functions like Book.class. 我想你们很容易猜到我有一个名为Author的类,它具有与Book.class类似的功能。
由于您正在扩展CrudRepository,因此save方法将使用Book,这意味着save(Book实体)
Is there a particular reason you want to override the save method in CrudRepository? 您是否有特定原因要覆盖CrudRepository中的save方法? The way Spring data works is that it will automagically implement any interface extensions of the CrudRepository interface and provide you with out the box methods for saving entities so you shouldn't really need to override it. Spring数据的工作方式是,它将自动实现CrudRepository接口的任何接口扩展,并为您提供开箱即用的方法来保存实体,因此您实际上不需要覆盖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.