英文:
Getting null value of Name and Price Property and Saving to Mysql using Spring JPARepository
问题
我面临获取名称和价格属性的空值以及在Spring Boot JPA中使用JpaRepository接口保存到MySQL时的问题。它只保存了cid字段到MySQL数据库,对于名称和价格字段,保存了null值。我正在使用MySQL数据库。
POJO类
@Data
@Entity
@Table(name = "COURSE_DTLS")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cid;
private String name;
private Double price;
}
Repository类
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import in.ashokit.binding.Course;
public interface CourseRepository extends JpaRepository<Course, Serializable> {
}
Service类
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CourseRepository courseRepo;
@Override
public String upsert(Course course) {
courseRepo.save(course); // upsert (insert / update based on PK)
return "success";
}
@Override
public Course getById(Integer cid) {
Optional<Course> findById = courseRepo.findById(cid);
if (findById.isPresent()) {
return findById.get();
}
return null;
}
@Override
public List<Course> getAllCourses() {
return courseRepo.findAll();
}
@Override
public String deleteById(Integer cid) {
if (courseRepo.existsById(cid)) {
courseRepo.deleteById(cid);
return "Delete Success";
} else {
return "No Record Found";
}
}
}
Controller类
@RestController
public class CourseRestController {
@Autowired
private CourseService courseService;
@PostMapping("/course")
public ResponseEntity<String> createCourse(@RequestBody Course course) {
String status = courseService.upsert(course);
return new ResponseEntity<>(status, HttpStatus.CREATED);
}
@GetMapping("/course/{cid}")
public ResponseEntity<Course> getCourse(@PathVariable Integer cid) {
Course course = courseService.getById(cid);
return new ResponseEntity<>(course, HttpStatus.OK);
}
@GetMapping("/courses")
public ResponseEntity<List<Course>> getAllCourses() {
List<Course> allCourses = courseService.getAllCourses();
return new ResponseEntity<>(allCourses, HttpStatus.OK);
}
@PutMapping("/course")
public ResponseEntity<String> updateCourse(@RequestBody Course course) {
String status = courseService.upsert(course);
return new ResponseEntity<>(status, HttpStatus.OK);
}
@DeleteMapping("/course/{cid}")
public ResponseEntity<String> deleteCourse(@PathVariable Integer cid) {
String status = courseService.deleteById(cid);
return new ResponseEntity<>(status, HttpStatus.OK);
}
}
英文:
I am facing issue regarding getting null value of Name and Price Property and Saving to Mysql using Spring bootJPA using interface JpaRepository. It saved data only cid to mysql db for all other fields saving null for Name and price fields in MySql Db. I am using MySql database.
POJO Class
@Data
@Entity
@Table(name = "COURSE_DTLS")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cid;
private String name;
private Double price;
}
Repo Class
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import in.ashokit.binding.Course;
public interface CourseRepository extends JpaRepository<Course, Serializable> {
}
Service class
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CourseRepository courseRepo;
@Override
public String upsert(Course course) {
courseRepo.save(course); // upsert (insert / update based on PK)
return "success";
}
@Override
public Course getById(Integer cid) {
Optional<Course> findById = courseRepo.findById(cid);
if (findById.isPresent()) {
return findById.get();
}
return null;
}
@Override
public List<Course> getAllCourses() {
return courseRepo.findAll();
}
@Override
public String deleteById(Integer cid) {
if (courseRepo.existsById(cid)) {
courseRepo.deleteById(cid);
return "Delete Success";
} else {
return "No Record Found";
}
}
}
Controller class
@RestController
public class CourseRestController {
@Autowired
private CourseService courseService;
@PostMapping("/course")
public ResponseEntity<String> createCourse(@RequestBody Course course) {
String status = courseService.upsert(course);
return new ResponseEntity<>(status, HttpStatus.CREATED);
}
@GetMapping("/course/{cid}")
public ResponseEntity<Course> getCourse(@PathVariable Integer cid) {
Course course = courseService.getById(cid);
return new ResponseEntity<>(course, HttpStatus.OK);
}
@GetMapping("/courses")
public ResponseEntity<List<Course>> getAllCourses() {
List<Course> allCourses = courseService.getAllCourses();
return new ResponseEntity<>(allCourses, HttpStatus.OK);
}
@PutMapping("/course")
public ResponseEntity<String> updateCourse(@RequestBody Course course) {
String status = courseService.upsert(course);
return new ResponseEntity<>(status, HttpStatus.OK);
}
@DeleteMapping("/course/{cid}")
public ResponseEntity<String> deleteCourse(@PathVariable Integer cid) {
String status = courseService.deleteById(cid);
return new ResponseEntity<>(status, HttpStatus.OK);
}
}
答案1
得分: 1
你的代码库有误。
将这部分代码:
JpaRepository<Course, Serializable>
修改为:
JpaRepository<Course, Integer>
英文:
Your repository is wrong.
Change this:
JpaRepository<Course, Serializable>
Like this:
JpaRepository<Course, Integer>
答案2
得分: 1
JpaRepository
中的第二个值应该是实体类主键的类型,在您的情况下是 Integer。请按以下方式更改 Repo
类:
public interface CourseRepository extends JpaRepository<Course, Integer> {
}
英文:
The second value in JpaRepository
should be the type of primary key of the entity class which in your case is Integer. Change Repo
class as follows:
public interface CourseRepository extends JpaRepository<Course, Integer> {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论