I am trying to make a project in spring-boot! But in the controller, I am getting an error in the
save()
method as follows:
The method save(S) in the type
CrudRepository<demoapplication,integer>
is not applicable for the arguments
(DemoApplication)Java(67108979)
-----------------------------------------------------------------------------
Here is the main App code:
package com.practice.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
--------------------------------------------------------------------------
Here is the entity code:
package com.practice.demo.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="practice_with_spring")
public class DemoApplication {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "name")
private String name;
@Column(name = "password")
private int password;
@Column(name = "age")
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getpassword() {
return password;
}
public void setpassword(int password) {
this.password = password;
}
public String getage() {
return age;
}
public void setage(String age) {
this.age = age;
}
}
-------------------------------------------------------------------------
Here is the
Controller
code:
package com.practice.demo.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.practice.demo.DemoApplication;
import com.practice.demo.repository.DemoApplicationRepository;
import org.springframework.beans.factory.annotation.Autowired;
@RestController
@RequestMapping(value="/DemoApplication")
public class DemoApplicationController {
@Autowired
private DemoApplicationRepository DemoApplicationRepository;
@PostMapping("/save")
DemoApplicationController createOrSaveEmployee(@RequestBody DemoApplication DemoApplication) {
return DemoApplicationRepository.save( DemoApplication);
}
}
---------------------------------------------------------------------------
Here is the
Repository
code:
package com.practice.demo.repository;
import com.practice.demo.entity.DemoApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DemoApplicationRepository extends JpaRepository<demoapplication,integer>{
}
-----------------------------------------------------------------------
This is the line which gives me an Error:
return DemoApplicationRepository.save( DemoApplication);
What I have tried:
I tried to solve this problem by myself, but I wasted more hours in this error than I expected. Moreover, i am a newbie in spring-boot projects. So can someone please help me!