SpringBoot JPA无法保存对象。

huangapple go评论103阅读模式
英文:

SpringBoot JPA can not save object

问题

这是错误信息:

  1. 2020-10-13 22:42:02.828 ERROR 15044 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property hotel found for type hotel!] with root cause

HotelDAO 对象:

  1. package rc.springbootmongodbdemo.DAO;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import rc.springbootmongodbdemo.Entities.hotel;
  5. import rc.springbootmongodbdemo.Entities.hsideReservation;
  6. import java.util.Collection;
  7. import java.util.Optional;
  8. @Component
  9. public class HotelDAO {
  10. @Autowired
  11. private HotelRepository repository;
  12. public hotel createHotel(hotel newHotel) {
  13. return repository.insert(newHotel);
  14. }
  15. public Collection<hsideReservation> hotelDeleteReservation(int hotel_id, hsideReservation intendedReservation){
  16. Optional<hotel> hotel = repository.findById(hotel_id);
  17. if(hotel.isPresent()){
  18. hotel.get().deleteReservation(intendedReservation);
  19. repository.save(hotel.get());
  20. return hotel.get().getHotelSideReservations();
  21. }
  22. else{
  23. System.out.println("Hotel is not registered");
  24. return null;
  25. }
  26. }
  27. public Collection<hsideReservation> newResforHotel(int hotel_id, hsideReservation newReservation){
  28. Optional<hotel> thisHotel = repository.findById(hotel_id);
  29. if(thisHotel.isPresent()){
  30. System.out.println(thisHotel.get().getHotelSideReservations().size());
  31. thisHotel.get().addReservation(newReservation);
  32. System.out.println(thisHotel.get().getHotelSideReservations().size());
  33. //This is where the issue originates
  34. repository.save(thisHotel.get());
  35. return thisHotel.get().getHotelSideReservations();
  36. }
  37. else return null;
  38. }
  39. }

Hotel 对象:

  1. package rc.springbootmongodbdemo.Entities;
  2. import org.springframework.data.annotation.Id;
  3. import org.springframework.data.mongodb.core.mapping.Document;
  4. import java.util.Collection;
  5. import java.util.List;
  6. public class hotel {
  7. @Id
  8. private int hotel_id;
  9. private String city;
  10. private List<hsideReservation> hotelSideReservations;
  11. public String getCity(){
  12. return city;
  13. }
  14. public int getHotel_id(){
  15. return hotel_id;
  16. }
  17. public Collection<hsideReservation> getHotelSideReservations(){
  18. return hotelSideReservations;
  19. }
  20. public Collection<hsideReservation> addReservation(hsideReservation newReservation){
  21. hotelSideReservations.add(newReservation);
  22. return hotelSideReservations;
  23. }
  24. public Collection<hsideReservation> deleteReservation(hsideReservation intendedRes) {
  25. for(int i = 0; i < hotelSideReservations.size(); i++){
  26. if(hotelSideReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
  27. hotelSideReservations.remove(i);
  28. return hotelSideReservations;
  29. }
  30. }
  31. return hotelSideReservations;
  32. }
  33. public String toString(){
  34. StringBuilder s = new StringBuilder();
  35. for(hsideReservation h : hotelSideReservations) {
  36. s.append(h.getDate());
  37. s.append(" , ");
  38. }
  39. return s.toString();
  40. }
  41. }

Customer 对象:

  1. package rc.springbootmongodbdemo.Entities;
  2. import org.springframework.data.annotation.Id;
  3. import java.util.Collection;
  4. import java.util.List;
  5. public class Customer {
  6. @Id
  7. private int id;
  8. private List<hotel_reservations> customerReservations;
  9. public Integer getId(){
  10. return id;
  11. }
  12. public Collection<hotel_reservations> getCustomerReservations(){
  13. return customerReservations;
  14. }
  15. public Collection<hotel_reservations> makeReservation(hotel_reservations reservation){
  16. customerReservations.add(reservation);
  17. return customerReservations;
  18. }
  19. public Collection<hotel_reservations> updateNewReservation(hotel_reservations editedRes){
  20. for(int i = 0; i < customerReservations.size(); i++){
  21. if(customerReservations.get(i).getReservation_id() == editedRes.getReservation_id()){
  22. customerReservations.get(i).set(editedRes);
  23. return customerReservations;
  24. }
  25. }
  26. return customerReservations;
  27. }
  28. public Collection<hotel_reservations> removeReservation(hotel_reservations intendedRes){
  29. for(int i = 0; i < customerReservations.size(); i++){
  30. if(customerReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
  31. customerReservations.remove(i);
  32. return customerReservations;
  33. }
  34. }
  35. return customerReservations;
  36. }
  37. public String toString(){
  38. StringBuilder s = new StringBuilder();
  39. for(hotel_reservations h : customerReservations) {
  40. s.append(h.getDate());
  41. s.append(" , ");
  42. }
  43. return s.toString();
  44. }
  45. }

CustomerDAO 对象:

  1. package rc.springbootmongodbdemo.DAO;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import rc.springbootmongodbdemo.Entities.Customer;
  6. import rc.springbootmongodbdemo.Entities.hotel_reservations;
  7. import java.util.Arrays;
  8. import java.util.Collection;
  9. import java.util.List;
  10. import java.util.Optional;
  11. @Component
  12. public class CustomerDAO {
  13. @Autowired
  14. private CustomerRepository repository;
  15. public Collection<hotel_reservations> getReservations(@PathVariable("id") int id){
  16. Optional<Customer> customer = repository.findById(id);
  17. if(customer.isPresent()){
  18. return customer.get().getCustomerReservations();
  19. }
  20. else{
  21. System.out.println("Customer is not registered");
  22. return null;
  23. }
  24. }
  25. public Customer createCustomer(Customer newCustomer) {
  26. return repository.insert(newCustomer);
  27. }
  28. public Collection<hotel_reservations> customerUpdateReservation(int id, hotel_reservations newReservation){
  29. Optional<Customer> customer = repository.findById(id);
  30. if(customer.isPresent()){
  31. customer.get().updateNewReservation(newReservation);
  32. repository.save(customer.get());
  33. return customer.get().getCustomerReservations();
  34. }
  35. else{
  36. System.out.println("Customer is not registered");
  37. return null;
  38. }
  39. }
  40. public Collection<hotel_reservations> createNewCustomerReservation(int customerId, hotel_reservations newReservation) {
  41. Optional<Customer> customer = repository.findById(customerId);
  42. if(customer.isPresent()){
  43. customer.get().makeReservation(newReservation);
  44. repository.save(customer.get());
  45. return customer.get().getCustomerReservations();
  46. }
  47. else{
  48. System.out.println("Customer is not registered");
  49. return null;
  50. }
  51. }
  52. public Collection<hotel_reservations> customerDeleteReservation(int id, hotel_reservations newReservation){
  53. Optional<Customer> customer = repository.findById(id);
  54. if(customer.isPresent()){
  55. customer.get().removeReservation(newReservation);
  56. repository.save(customer.get());
  57. return customer.get().getCustomerReservations();
  58. }
  59. else{
  60. System.out.println("Customer is not registered");
  61. return null;
  62. }
  63. }
  64. }
英文:

I have an API that wants to save a hotel object after a modification. The hotel object is structured exactly the same as another customer object, however when I try to get and save a hotel object springboot is throwing a property reference exception. My customer object works perfectly fine so I am not sure what is different. I have already tried to rename the hotel class to "Hotels" and "Hotel", and it did not work.
This is the error:

  1. 2020-10-13 22:42:02.828 ERROR 15044 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property hotel found for type hotel!] with root cause

HotelDAO Object:

  1. package rc.springbootmongodbdemo.DAO;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import rc.springbootmongodbdemo.Entities.hotel;
  5. import rc.springbootmongodbdemo.Entities.hsideReservation;
  6. import java.util.Collection;
  7. import java.util.Optional;
  8. @Component
  9. public class HotelDAO {
  10. @Autowired
  11. private HotelRepository repository;
  12. public hotel createHotel(hotel newHotel) {
  13. return repository.insert(newHotel);
  14. }
  15. public Collection&lt;hsideReservation&gt; hotelDeleteReservation(int hotel_id, hsideReservation intendedReservation){
  16. Optional&lt;hotel&gt; hotel = repository.findById(hotel_id);
  17. if(hotel.isPresent()){
  18. hotel.get().deleteReservation(intendedReservation);
  19. repository.save(hotel.get());
  20. return hotel.get().getHotelSideReservations();
  21. }
  22. else{
  23. System.out.println(&quot;Hotel is not registered&quot;);
  24. return null;
  25. }
  26. }
  27. public Collection&lt;hsideReservation&gt; newResforHotel(int hotel_id, hsideReservation newReservation){
  28. Optional&lt;hotel&gt; thisHotel = repository.findById(hotel_id);
  29. if(thisHotel.isPresent()){
  30. System.out.println(thisHotel.get().getHotelSideReservations().size());
  31. thisHotel.get().addReservation(newReservation);
  32. System.out.println(thisHotel.get().getHotelSideReservations().size());
  33. //This is where the issue originates
  34. repository.save(thisHotel.get());
  35. return thisHotel.get().getHotelSideReservations();
  36. }
  37. else return null;
  38. }
  39. }

Hotel Object

  1. package rc.springbootmongodbdemo.Entities;
  2. import org.springframework.data.annotation.Id;
  3. import org.springframework.data.mongodb.core.mapping.Document;
  4. import java.util.Collection;
  5. import java.util.List;
  6. public class hotel {
  7. @Id
  8. private int hotel_id;
  9. private String city;
  10. private List&lt;hsideReservation&gt; hotelSideReservations;
  11. public String getCity(){
  12. return city;
  13. }
  14. public int getHotel_id(){
  15. return hotel_id;
  16. }
  17. public Collection&lt;hsideReservation&gt; getHotelSideReservations(){
  18. return hotelSideReservations;
  19. }
  20. public Collection&lt;hsideReservation&gt; addReservation(hsideReservation newReservation){
  21. hotelSideReservations.add(newReservation);
  22. return hotelSideReservations;
  23. }
  24. public Collection&lt;hsideReservation&gt; deleteReservation(hsideReservation intendedRes) {
  25. for(int i = 0; i &lt; hotelSideReservations.size(); i++){
  26. if(hotelSideReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
  27. hotelSideReservations.remove(i);
  28. return hotelSideReservations;
  29. }
  30. }
  31. return hotelSideReservations;
  32. }
  33. public String toString(){
  34. StringBuilder s = new StringBuilder();
  35. for(hsideReservation h : hotelSideReservations) {
  36. s.append(h.getDate());
  37. s.append(&quot; , &quot;);
  38. }
  39. return s.toString();
  40. }
  41. }

Customer Object:

  1. package rc.springbootmongodbdemo.Entities;
  2. import org.springframework.data.annotation.Id;
  3. import java.util.Collection;
  4. import java.util.List;
  5. public class Customer {
  6. @Id
  7. private int id;
  8. private List&lt;hotel_reservations&gt; customerReservations;
  9. public Integer getId(){
  10. return id;
  11. }
  12. public Collection&lt;hotel_reservations&gt; getCustomerReservations(){
  13. return customerReservations;
  14. }
  15. public Collection&lt;hotel_reservations&gt; makeReservation(hotel_reservations reservation){
  16. customerReservations.add(reservation);
  17. return customerReservations;
  18. }
  19. public Collection&lt;hotel_reservations&gt; updateNewReservation(hotel_reservations editedRes){
  20. for(int i = 0; i &lt; customerReservations.size(); i++){
  21. if(customerReservations.get(i).getReservation_id() == editedRes.getReservation_id()){
  22. customerReservations.get(i).set(editedRes);
  23. return customerReservations;
  24. }
  25. }
  26. return customerReservations;
  27. }
  28. public Collection&lt;hotel_reservations&gt; removeReservation(hotel_reservations intendedRes){
  29. for(int i = 0; i &lt; customerReservations.size(); i++){
  30. if(customerReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
  31. customerReservations.remove(i);
  32. return customerReservations;
  33. }
  34. }
  35. return customerReservations;
  36. }
  37. public String toString(){
  38. StringBuilder s = new StringBuilder();
  39. for(hotel_reservations h : customerReservations) {
  40. s.append(h.getDate());
  41. s.append(&quot; , &quot;);
  42. }
  43. return s.toString();
  44. }
  45. }

CustomerDAO Object:

  1. package rc.springbootmongodbdemo.DAO;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import rc.springbootmongodbdemo.Entities.Customer;
  6. import rc.springbootmongodbdemo.Entities.hotel_reservations;
  7. import java.util.Arrays;
  8. import java.util.Collection;
  9. import java.util.List;
  10. import java.util.Optional;
  11. @Component
  12. public class CustomerDAO {
  13. @Autowired
  14. private CustomerRepository repository;
  15. //List of customer reservations
  16. public Collection&lt;hotel_reservations&gt; getReservations(@PathVariable(&quot;id&quot;) int id){
  17. Optional&lt;Customer&gt; customer = repository.findById(id);
  18. if(customer.isPresent()){
  19. return customer.get().getCustomerReservations();
  20. }
  21. else{
  22. System.out.println(&quot;Customer is not registered&quot;);
  23. return null;
  24. }
  25. }
  26. public Customer createCustomer(Customer newCustomer) {
  27. return repository.insert(newCustomer);
  28. }
  29. public Collection&lt;hotel_reservations&gt; customerUpdateReservation(int id, hotel_reservations newReservation){
  30. Optional&lt;Customer&gt; customer = repository.findById(id);
  31. if(customer.isPresent()){
  32. customer.get().updateNewReservation(newReservation);
  33. repository.save(customer.get());
  34. return customer.get().getCustomerReservations();
  35. }
  36. else{
  37. System.out.println(&quot;Customer is not registered&quot;);
  38. return null;
  39. }
  40. }
  41. public Collection&lt;hotel_reservations&gt; createNewCustomerReservation(int customerId, hotel_reservations newReservation) {
  42. Optional&lt;Customer&gt; customer = repository.findById(customerId);
  43. if(customer.isPresent()){
  44. customer.get().makeReservation(newReservation);
  45. repository.save(customer.get());
  46. return customer.get().getCustomerReservations();
  47. }
  48. else{
  49. System.out.println(&quot;Customer is not registered&quot;);
  50. return null;
  51. }
  52. }
  53. public Collection&lt;hotel_reservations&gt; customerDeleteReservation(int id, hotel_reservations newReservation){
  54. Optional&lt;Customer&gt; customer = repository.findById(id);
  55. if(customer.isPresent()){
  56. customer.get().removeReservation(newReservation);
  57. repository.save(customer.get());
  58. return customer.get().getCustomerReservations();
  59. }
  60. else{
  61. System.out.println(&quot;Customer is not registered&quot;);
  62. return null;
  63. }
  64. }
  65. }

答案1

得分: 0

Spring在实现时使用标准命名约定。

请纠正以下内容并检查是否解决。

  1. 将类名hotel重命名为Hotel

  2. 将hotel_id重命名为hotel_id

  3. 将hotel_reservations重命名为HotelReservations

使用Java标准命名约定以避免任何异常问题。

英文:

Spring uses standard naming conventions when it comes to implementation.

Please correct below things and check whether it gets resolved.

  1. Rename class name hotel to Hotel

  2. Rename hotel_id to hotel is

  3. Rename hotel_reservations to HotelReservations

Use java standard naming conventions to avoid any abnormal issues.

huangapple
  • 本文由 发表于 2020年10月14日 10:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64345978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定