Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix the CrudRepository.save(java.lang.Object) is no accessor method in springboot?

im reffering to this springboot tutorial and im using spring data in my project, im trying to add data to database . using the following . bt when im trying to do that i get an error saying

Invoked method public abstract java.lang.Object org.springframework.data.repository.CrudRepository.save(java.lang.Object) is no accessor method!

here is my code ,

//my controller

@RequestMapping("/mode")
    public String showProducts(ModeRepository repository){
        Mode m = new Mode();
        m.setSeats(2);
        repository.save(m); //this is where the error getting from
        return "product";
    }


//implementing crud with mode repository
@Repository
public interface ModeRepository extends CrudRepository<Mode, Long> {

}

 //my mode class
@Entity
@Table(name="mode")
public class Mode implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private int idMode; 


    @Column(nullable=false)
    private int seats;

    //assume that there are getters and setters
}

im new to springboot and can someone tellme what am i doing wrong, appreciate if someone could provide me a link to get to know about springdata other than spring documentation

like image 681
Priyamal Avatar asked Dec 08 '22 22:12

Priyamal


1 Answers

Change your controller code so that the ModeRepository is a private autowired field.

    @Autowired //don't forget the setter
    private ModeRepository repository; 

    @RequestMapping("/mode")
    public String showProducts(){
        Mode m = new Mode();
        m.setSeats(2);
        repository.save(m); //this is where the error getting from
        return "product";
    }
like image 137
WeMakeSoftware Avatar answered Dec 10 '22 12:12

WeMakeSoftware