Java 使用JPA和Hibernate覆盖CrudeRepository save()错误

Java 使用JPA和Hibernate覆盖CrudeRepository save()错误,java,spring,hibernate,spring-data-jpa,Java,Spring,Hibernate,Spring Data Jpa,我试图使用Hibernate和JpaRepository来覆盖save函数,但每次我试图运行程序时,都会收到错误提示 错误:(15,23)com.studapp.StudentJpaRepository中的java:save(com.studapp.students.Student)与org.springframework.data.repository.crudepository中的save冲突 返回类型java.util.Optional与S不兼容 学生班级: @Entity @Table(

我试图使用Hibernate和JpaRepository来覆盖save函数,但每次我试图运行程序时,都会收到错误提示

错误:(15,23)com.studapp.StudentJpaRepository中的java:save(com.studapp.students.Student)与org.springframework.data.repository.crudepository中的save冲突 返回类型java.util.Optional与S不兼容

学生班级:

@Entity
@Table(name = "Student")
public class Student implements Serializable {

    @Id
    @Column(name = "STUDENT_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long ID;

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lasttName")
    private String lastName;

    @Column(name = "dateOfBirth")
    private LocalDate dateOfBirth;

    @Column(name = "JMBAG")
    private String JMBAG;

    @Column(name = "numberOfECTS")
    private Integer numberOfECTS;

    @ManyToMany(targetEntity = Course.class )
    @JoinTable(
            name = "student_course" ,
            joinColumns = { @JoinColumn(name = "STUDENT_ID") } ,
            inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") }
    )
    private List<Course> courses;

    public Student(){};

    public Student(String firstName, String lastName, LocalDate dateOfBirth, String jmbag, Integer numberOfECTS) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.dateOfBirth = dateOfBirth;
        this.JMBAG = jmbag;
        this.numberOfECTS = numberOfECTS;
    }
//getters and setters

为了覆盖现有方法,您必须创建与crudepository中完全相同的保存方法

您可以执行以下操作:

@Repository
public interface StudentJpaRepository extends JpaRepository<Student, Long> {
    <S extends MyEntity> S save(S entity);
}
@存储库
公共界面StudentJpaRepository扩展了JpaRepository{
S save(S实体);
}
MyCustomRepositoryImpl.java

public class MyCustomRepositoryImpl implements StudentJpaRepository {

    @Override
    public <S extends MyBean> S save(S entity) {
       /**
         your custom implementation comes here ...
       */
    }
公共类MyCustomRepositoryImpl实现StudentJpaRepository{ @凌驾 公共存储(S实体){ /** 您的自定义实现在这里。。。 */ }
}

为什么需要保存方法来返回可选值?Save方法返回保存的项(新创建的或更新的)

可选返回意味着返回的值可以为null。而且它不可能返回null

根据您在逻辑中的理解,您只需要基于唯一键(例如:FirstName LastName DateOfBirth)检查该项是否存在,如果存在,则返回
HttpStatus.CONFLICT


希望这有帮助

不是
save
方法返回可选结果,如果在保存过程中出现问题,最好抛出异常。但是如果我使用Hibernate呢?在这个存储库中,我有很多其他的方法。像findStudentByLastName(String lastName)等,然后我需要编写这些方法的实现,这些自定义方法在Crudepository中没有预定义。因此,您可以轻松地为他们编写Implementation类。只是遵循模式,以便用户更容易使用。
@Service
public class StudentServiceImpl implements StudentService{

    private static final int YEARS_AFTER_WHICH_TUITION_SHOULD_BE_PAYED = 30;

    private final StudentJpaRepository studentJpaRepository;

    public StudentServiceImpl(StudentJpaRepository studentJpaRepository){
        this.studentJpaRepository = studentJpaRepository;
    }

    @Override
    public Optional<StudentDTO> save(final StudentCommand command) {
        return studentJpaRepository.save(mapCommmandToStudent(command)).map(this::mapStudentToDTO);
    }

    private StudentDTO mapStudentToDTO(final Student student){
        return new StudentDTO (student.getFirstName(), student.getLastName(), 
    student.getJMBAG(),student.getNumberOfECTS(), shouldTuitionBePayed(student.getDateOfBirth()));
    }

    private Student mapCommmandToStudent(final StudentCommand studentCommand) {
        return new Student(studentCommand.getFirstName(), studentCommand.getLastName(), 
    studentCommand.getDateOfBirth(), studentCommand.getJmbag(), studentCommand.getNumberOfECTS());
    }
}
@Repository
public interface StudentJpaRepository extends JpaRepository<Student, Long> {
    Optional<Student> save(Student student);
}
CREATE TABLE IF NOT EXISTS Student (
    STUDENT_ID identity,
    JMBAG CHAR(10) NOT NULL ,
    firstName VARCHAR(50) NOT NULL,
    lastName VARCHAR(50) NOT NULL,
    dateOfBirth DATE NOT NULL,
    numberOfECTS INT NOT NULL
) ;
INSERT INTO Student ( firstName, lastName, dateOfBirth, JMBAG, numberOfECTS)
VALUES ('Jhon' , 'Jhonson' , '1993-05-08' , '0024789510' , 121  );
@Repository
public interface StudentJpaRepository extends JpaRepository<Student, Long> {
    <S extends MyEntity> S save(S entity);
}
public class MyCustomRepositoryImpl implements StudentJpaRepository {

    @Override
    public <S extends MyBean> S save(S entity) {
       /**
         your custom implementation comes here ...
       */
    }