【发布时间】: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