简体   繁体   English

两种方法具有相同的擦除,但是都不能覆盖另一种方法

[英]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.

相关问题 两种方法都有相同的擦除,但都没有覆盖另一个 - Both methods have same erasure yet neither overrides the other “'Mapper' 中的 map(From)' 与 'CursorToMessageImpl' 中的 'map(Object)' 冲突;两种方法具有相同的擦除,但都不会覆盖另一个 - "map(From)' in 'Mapper' clashes with 'map(Object)' in 'CursorToMessageImpl'; both methods have same erasure, yet neither overrides the other 在实现ConsumerSeekAware接口时,如何使“这两种方法都具有相同的擦除能力,但两者都不能覆盖另一个方法”的警告静音? - How do I silence the “both methods have same erasure yet neither overrides the other” warning while implementing the ConsumerSeekAware interface? 无法覆盖onBeforeConvert:“......有相同的删除,但都没有覆盖其他” - Cannot Override onBeforeConvert: “…have the same erasure, yet neither overrides the other” 集具有相同的擦除,但没有一个覆盖另一个错误 - Set have the same erasure, yet neither overrides the other error SkuDetailsResponseListener() 中的“两种方法都具有相同的擦除功能,但都不会覆盖另一个”方法冲突错误 - 'Both methods have same erasure, yet neither overides the other' method clash error in SkuDetailsResponseListener() 实现Comparable,compareTo名称冲突:“具有相同的擦除,但不会覆盖其他” - Implementing Comparable, compareTo name clash: “have the same erasure, yet neither overrides the other” 名称冲突 - 具有相同的擦除但在方法参数generic中不会覆盖另一个 - Name clash - have the same erasure yet neither overrides the other in method parameter generic 具有相同的擦除,但不能覆盖仅在一种情况下出现的其他警告 - Have same erasure, yet neither overrides the other warning appearing in only ONE situation 界面和一个类。名称冲突:相同的擦除,但都不会覆盖其他 - interface and a class. name clash: same erasure, yet neither overrides other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM