我有兩個表的數據庫:春庫查詢兩個表
1)NumberEntity
|------|-------|--------|
| id |numero | volume |
|------|-------|--------|
| 1 | 1 | 1 |
|------|-------|--------|
| 2 | 1 | 2 |
|------|-------|--------|
| 3 | 2 | 1 |
|------|-------|--------|
2)ArticleEntity
|------|-------|------------|
| id |Article| numbers_id |
|------|-------|------------|
| 5 | 7 | 1 |
|------|-------|------------|
| 6 | 5 | 2 |
|------|-------|------------|
| 7 | 6 | 3 |
|------|-------|------------|
哪裏numbers_id是第一個表的關係。 我想解壓縮通過第一個 表,文章desc訂購的文章。 我不知道我該怎麼辦呢,我開始與此查詢:
public List<NumberEntity> findByVolumeAndNumero(String volume, String number);
我的文章的列表中,但首先是文章數7,而不是我想萃取,如第一篇文章數字5,6和7
這是我的模型:
@Entity
public class NumberEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String volume;
private String numero;
@OneToMany(mappedBy="numbers", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ArticleEntity> articles = new ArrayList<ArticleEntity>();
另:
@Entity
public class ArticleEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String article;
@ManyToOne
private NumberEntity numbers;
所以我需要這樣一個查詢(即使itsn't糾正,但它只能是僞代碼):
public List<NumberEntity> findByVolumeAndNumeroOrderByArticleDesc(String volume, String number);
的問題是,我不知道如何加入其他表用使用彈簧的單個查詢
https://stackoverflow.com/questions/43891571/crudrepository-join-single-field-from-other-table-to-entity-as-readonly.please檢查此鏈接。 – Ram