繁体   English   中英

SpringBoot JPA 无法保存对象

[英]SpringBoot JPA can not save object

我有一个 API 想要在修改后保存酒店对象。 酒店对象的结构与另一个客户对象完全相同,但是当我尝试获取和保存酒店对象时,springboot 会引发属性引用异常。 我的客户对象工作得很好,所以我不确定有什么不同。 我已经尝试将酒店类重命名为“Hotels”和“Hotel”,但没有成功。 这是错误:

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 对象:

package rc.springbootmongodbdemo.DAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rc.springbootmongodbdemo.Entities.hotel;
import rc.springbootmongodbdemo.Entities.hsideReservation;

import java.util.Collection;
import java.util.Optional;

@Component
public class HotelDAO {
    @Autowired
    private HotelRepository repository;

    public hotel createHotel(hotel newHotel) {
        return repository.insert(newHotel);
    }

    public Collection<hsideReservation> hotelDeleteReservation(int hotel_id, hsideReservation intendedReservation){
        Optional<hotel> hotel = repository.findById(hotel_id);
        if(hotel.isPresent()){
            hotel.get().deleteReservation(intendedReservation);
            repository.save(hotel.get());
            return hotel.get().getHotelSideReservations();
        }
        else{
            System.out.println("Hotel is not registered");
            return null;
        }
    }

    public Collection<hsideReservation> newResforHotel(int hotel_id, hsideReservation newReservation){
        Optional<hotel> thisHotel = repository.findById(hotel_id);
        if(thisHotel.isPresent()){
            System.out.println(thisHotel.get().getHotelSideReservations().size());
            thisHotel.get().addReservation(newReservation);
            System.out.println(thisHotel.get().getHotelSideReservations().size());
            //This is where the issue originates
            repository.save(thisHotel.get());
            return thisHotel.get().getHotelSideReservations();
        }
        else return null;
    }

}

酒店对象

package rc.springbootmongodbdemo.Entities;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Collection;
import java.util.List;
public class hotel {
    @Id
    private int hotel_id;
    private String city;
    private List<hsideReservation> hotelSideReservations;

    public String getCity(){
        return city;
    }
    public int getHotel_id(){
        return hotel_id;
    }
    public Collection<hsideReservation> getHotelSideReservations(){
        return hotelSideReservations;
    }
    public Collection<hsideReservation> addReservation(hsideReservation newReservation){
        hotelSideReservations.add(newReservation);
        return hotelSideReservations;
    }


    public Collection<hsideReservation> deleteReservation(hsideReservation intendedRes) {
        for(int i = 0; i < hotelSideReservations.size(); i++){
            if(hotelSideReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
                hotelSideReservations.remove(i);
                return hotelSideReservations;
            }
        }
        return hotelSideReservations;
    }
    public String toString(){
        StringBuilder s = new StringBuilder();
        for(hsideReservation h : hotelSideReservations) {
            s.append(h.getDate());
            s.append(" , ");
        }
        return s.toString();
    }

}


客户对象:

package rc.springbootmongodbdemo.Entities;

import org.springframework.data.annotation.Id;

import java.util.Collection;
import java.util.List;

public class Customer {
    @Id
    private int id;
    private List<hotel_reservations> customerReservations;

    public Integer getId(){
        return id;
    }
    public Collection<hotel_reservations> getCustomerReservations(){
        return customerReservations;
    }

    public Collection<hotel_reservations> makeReservation(hotel_reservations reservation){
        customerReservations.add(reservation);
        return customerReservations;
    }

    public Collection<hotel_reservations> updateNewReservation(hotel_reservations editedRes){
        for(int i = 0; i < customerReservations.size(); i++){
            if(customerReservations.get(i).getReservation_id() == editedRes.getReservation_id()){
                customerReservations.get(i).set(editedRes);
                return customerReservations;
            }
        }
        return customerReservations;
        }

    public Collection<hotel_reservations> removeReservation(hotel_reservations intendedRes){
        for(int i = 0; i < customerReservations.size(); i++){
            if(customerReservations.get(i).getReservation_id() == intendedRes.getReservation_id()){
                customerReservations.remove(i);
                return customerReservations;
            }
        }
        return customerReservations;
    }

   public String toString(){
        StringBuilder s = new StringBuilder();
        for(hotel_reservations h : customerReservations) {
            s.append(h.getDate());
            s.append(" , ");
        }
        return s.toString();
   }
}

CustomerDAO 对象:

package rc.springbootmongodbdemo.DAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import rc.springbootmongodbdemo.Entities.Customer;
import rc.springbootmongodbdemo.Entities.hotel_reservations;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;


@Component
public class CustomerDAO {
    @Autowired
    private CustomerRepository repository;
    //List of customer reservations
    public Collection<hotel_reservations> getReservations(@PathVariable("id") int id){
        Optional<Customer> customer = repository.findById(id);
        if(customer.isPresent()){
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }

    }

    public Customer createCustomer(Customer newCustomer) {
        return repository.insert(newCustomer);
    }

    public Collection<hotel_reservations> customerUpdateReservation(int id, hotel_reservations newReservation){
        Optional<Customer> customer = repository.findById(id);
        if(customer.isPresent()){
            customer.get().updateNewReservation(newReservation);
            repository.save(customer.get());
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }
    }

    public Collection<hotel_reservations> createNewCustomerReservation(int customerId, hotel_reservations newReservation) {
        Optional<Customer> customer = repository.findById(customerId);
        if(customer.isPresent()){
            customer.get().makeReservation(newReservation);
            repository.save(customer.get());
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }
    }
    public Collection<hotel_reservations> customerDeleteReservation(int id, hotel_reservations newReservation){
        Optional<Customer> customer = repository.findById(id);
        if(customer.isPresent()){
            customer.get().removeReservation(newReservation);
            repository.save(customer.get());
            return customer.get().getCustomerReservations();
        }
        else{
            System.out.println("Customer is not registered");
            return null;
        }
    }
}

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

请更正以下内容并检查它是否得到解决。

  1. 将类名酒店重命名为酒店

  2. 将hotel_id重命名为hotel is

  3. 将 hotel_reservations 重命名为 HotelReservations

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

列表中的 findBy 属性<object> SpringBoot JPA 存储库<div id="text_translate"><p>我的数据库 Story 和 Tag 中的两个对象之间存在一对多关系。</p><p> 我希望能够获取所有带有标签 object 和字符串名称的 Story 对象。</p><p> 故事.java</p><pre> @Entity @Table(name = "stories") public class Story { @Id @GeneratedValue private Long id; @Column(name = "title") private String title; @JsonIgnoreProperties({"story"}) @OneToMany(mappedBy = "story", fetch = FetchType.LAZY) private List&lt;Tag&gt; tags; public Story(String title){ this.title = title; } public Story(){ } // getters &amp; setters }</pre><p> 标签.java</p><pre> @Entity @Table(name = "tags") public class Tag { @Id @GeneratedValue private Long id; @Column(name = "name") private String name; @JsonIgnoreProperties({"tags"}) @ManyToOne @JoinColumn(name = "story_id", nullable = false) @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) private Story story; public Tag(String name, Story story){ this.name = name; this.story = story; } public Tag(){ } /// getters &amp; setters }</pre><p> StoryController.java</p><pre> @RestController public class StoryController { @Autowired StoryRepository storyRepository; @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value = "/stories") public ResponseEntity&lt;List&lt;Story&gt;&gt; getAllStories(){ return new ResponseEntity&lt;&gt;(storyRepository.findAll(), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/tagSearch/{name}") public ResponseEntity&lt;Story&gt; getStoryByTag(@PathVariable String name) { return new ResponseEntity(storyRepository.findByTags_Name(name), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/{id}") public ResponseEntity&lt;Story&gt; getStory(@PathVariable Long id) { return new ResponseEntity(storyRepository.findById(id), HttpStatus.OK); } }</pre><p> StoryRepository.java</p><pre> @Repository public interface StoryRepository extends JpaRepository&lt;Story, Long&gt; { public List&lt;Story&gt; findByTags_Name(String name); }</pre><p> 尝试通过浏览器查询时,转到地址 localhost:8080/api/stories/tagSearch/?name="tag" 数据库返回数据库中的所有对象,而不是我要查找的结果。</p></div></object>

[英]findBy property in List<Object> SpringBoot JPA Repository

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在springboot中使用rest模板保存新对象 JPA SpringBoot - 无法将新实体保存到数据库 在SpringBoot JPA方法中将Java对象转换为字符串 列表中的 findBy 属性<object> SpringBoot JPA 存储库<div id="text_translate"><p>我的数据库 Story 和 Tag 中的两个对象之间存在一对多关系。</p><p> 我希望能够获取所有带有标签 object 和字符串名称的 Story 对象。</p><p> 故事.java</p><pre> @Entity @Table(name = "stories") public class Story { @Id @GeneratedValue private Long id; @Column(name = "title") private String title; @JsonIgnoreProperties({"story"}) @OneToMany(mappedBy = "story", fetch = FetchType.LAZY) private List&lt;Tag&gt; tags; public Story(String title){ this.title = title; } public Story(){ } // getters &amp; setters }</pre><p> 标签.java</p><pre> @Entity @Table(name = "tags") public class Tag { @Id @GeneratedValue private Long id; @Column(name = "name") private String name; @JsonIgnoreProperties({"tags"}) @ManyToOne @JoinColumn(name = "story_id", nullable = false) @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) private Story story; public Tag(String name, Story story){ this.name = name; this.story = story; } public Tag(){ } /// getters &amp; setters }</pre><p> StoryController.java</p><pre> @RestController public class StoryController { @Autowired StoryRepository storyRepository; @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value = "/stories") public ResponseEntity&lt;List&lt;Story&gt;&gt; getAllStories(){ return new ResponseEntity&lt;&gt;(storyRepository.findAll(), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/tagSearch/{name}") public ResponseEntity&lt;Story&gt; getStoryByTag(@PathVariable String name) { return new ResponseEntity(storyRepository.findByTags_Name(name), HttpStatus.OK); } @CrossOrigin(origins = "http://localhost:8080/api") @GetMapping(value="/stories/{id}") public ResponseEntity&lt;Story&gt; getStory(@PathVariable Long id) { return new ResponseEntity(storyRepository.findById(id), HttpStatus.OK); } }</pre><p> StoryRepository.java</p><pre> @Repository public interface StoryRepository extends JpaRepository&lt;Story, Long&gt; { public List&lt;Story&gt; findByTags_Name(String name); }</pre><p> 尝试通过浏览器查询时,转到地址 localhost:8080/api/stories/tagSearch/?name="tag" 数据库返回数据库中的所有对象,而不是我要查找的结果。</p></div></object> springboot jpa无法创建表 SpringBoot JPA接口的save()和delete()不能与ArrayList或Long一起使用 无法从 SpringBoot Main 方法保存 JPA Repository 变量的方法 无法在SpringBoot应用程序中使用JPA在数据库中保存数据 SpringBoot JPA 实体忽略时区,同时将日期保存到 PostgreSQL 如何将对象保存到另一个表Springboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM