Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
Java
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:
Java
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:
Java
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:
Java
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:

Java
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!
Posted
Updated 19-Aug-23 10:24am
v2

1 solution

The issue is with spring-data-jpa version, try changing to another version in pom.xml(if its maven) or you may use the below mentioned version if it works for your spring boot project version:

XML
<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>2.5.5</version>
		</dependency>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900