I am trying to save an object Rating which has another Mobile, so it includes a grade and a phone.
Here is mobile entity:
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Mobile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
}
Here is rating entity
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Rating {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Mobile mobile;
private int grade;
}
Method saveRating from RatingService class
public Rating saveRating(Rating rating) {
Rating theRating = new Rating();
Mobile theMobile = mobileRepo.getById(rating.getMobile().getId());
theRating.setGrade(rating.getGrade());
theRating.setMobile(theMobile);
return ratingRepo.save(theRating);
}
And method for saving in rest controller
@Autowired
private RatingService ratingService;
@RequestMapping(value = "/rating", method = RequestMethod.POST)
public Rating newRating(@RequestBody Rating rating){
return ratingService.saveRating(rating);
}
I have all @Service @RestController @Autowired annotations. Other stuff works as saving mobile, deleting it and viewing it, even viewing ratings which I manually added in database. I have tried variating newRating method in controller and in RatingService with it getting only the mobile id and grade and then finding mobile through mobileRepository and saving it as mobile for rating but I had not made progress with it, so I've returned to this method from witch I have started. It seems very simple to do this, but at the moment is not. Here is postman result: This is from postman