【问题标题】:Spring Data JPA ManyToOne query nullSpring Data JPA ManyToOne 查询 null
【发布时间】:2020-05-26 11:13:39
【问题描述】:

我已经开始学习 Spring Boot,我创建了两个实体 Invoice 和 YearDate

当我尝试按年份(id)搜索时,在我的日志查询中我得到了 null(请参阅日志查询的这一部分)

http://localhost:8080/appapi/invoices/search/findByYearId?year=1

from invoice invoice0_ left outer join year_date yeardate1_ on invoice0_.year_id=yeardate1_.id where yeardate1_.id is null limit?

我也将 Lombok 用于获取器和设置器

这是我所有的类实体、SQL 表和 JpaRepository 接口:

SQL 外键:

KEY `fk_year` (`year_id`),
  CONSTRAINT `fk_year` FOREIGN KEY (`year_id`) REFERENCES `YearDate` (`id`)

YearDate 类:

@Entity
@Table(name="YearDate")
@Data
public class YearDate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "year_value")
    private String yearValue;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "year")
    private Set<Invoice> invoices;
}

发票类:

@Entity
@Table(name="invoice")
@Data
public class Invoice {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @ManyToOne
    @JoinColumn(name = "year_id", nullable = false)
    private YearDate year;

    @Column(name = "description")
    private String description;
}


**And The Invoice Interface:**

@CrossOrigin("http://localhost:4200")
public interface InvoiceRepository extends JpaRepository<Invoice, Long> {

    Page<Invoice> findByYearId(@RequestParam("year") Long id, Pageable page);

}

【问题讨论】:

    标签: spring spring-boot spring-data-jpa


    【解决方案1】:

    为我工作。

    尝试在休息控制器中提取调用并记录参数

    @GetMapping(value = "/invoices")
      public Page<Invoice> getInvoices(@RequestParam("yearId") Long yearId) {
            Page<Invoice> byYearId = invoiceRepository.findByYearId(yearId, PageRequest.of(0, 10));
            return byYearId;
    }
    
    
    public interface InvoiceRepository extends JpaRepository<Invoice, Long> {
    
        Page<Invoice> findByYearId(Long id, Pageable page);
    }
    
    
     @ManyToOne
        @JoinColumn(name = "year_id", nullable = false)
       @JsonIgnoreProperties("invoices")
        private YearDate year;
    
     @OneToMany(mappedBy = "year")
        @JsonIgnoreProperties("year")
        private Set<Invoice> invoices;
    

    生成的 SQL:

    Hibernate: 
        select
            invoice0_.id as id1_0_,
            invoice0_.description as descript2_0_,
            invoice0_.name as name3_0_,
            invoice0_.year_id as year_id4_0_ 
        from
            invoice invoice0_ 
        left outer join
            year_date yeardate1_ 
                on invoice0_.year_id=yeardate1_.id 
        where
            yeardate1_.id=? limit ?
    Hibernate: 
        select
            yeardate0_.id as id1_5_0_,
            yeardate0_.year_value as year_val2_5_0_ 
        from
            year_date yeardate0_ 
        where
            yeardate0_.id=?
    

    -> 将@JsonIgnoreProperties("invoices") 和@JsonIgnoreProperties("year") 添加到实体以避免无限的json recursion。

    【讨论】:

    • 当我添加此更新时,我会像这样拨打电话:localhost:8080/appapi/invoices?yearId=1 & 我获得 500 内部服务器错误和无限日志,如下所示:故障安全清理(集合):org.hibernate .engine.loading.internal.CollectionLoadContext@5f5daaff
    【解决方案2】:

    我已禁用 Lombok 并且它可以工作

    【讨论】:

      【解决方案3】:

      试着给他一个 Year 对象而不是 id 更好

      @CrossOrigin("http://localhost:4200")
      public interface InvoiceRepository extends JpaRepository<Invoice, Long> {
          Page<Invoice> findByYear(YearDate year, Pageable page);
      }
      

      并在您的服务中获得今年 例如,我在服务中创建了一个 getYears 方法:

      @Service
      public class YearService{
      
      @Autowired
      private YearRepository yearRepository;
      @Autowired
      private InvoiceRepository invoiceRepository;
      
      getYears(idYear:Long){
      YearDate yearParam=yearRepository.findById(id).get();
      Page<Invoice> invoices=invoiceRepository.findByYear(yearParam,YourPagination)
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-04
        • 1970-01-01
        • 2021-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        • 2020-08-24
        • 1970-01-01
        相关资源
        最近更新 更多