繁体   English   中英

Spring 引导 JPA 查询 MySQL 只读取第一行

[英]Spring Boot JPA Query on MySQL reads only the first row

从 mysql 读取视图时,我遇到了一个奇怪的行为。 我的 select 语句只读取第一行。

但是当我阅读其他数据库表时它工作正常。

有人知道它可能是什么吗?

这是我的创建声明:

CREATE ALGORITHM=UNDEFINED DEFINER= root @ localhost SQL 安全定义器视图v_monthlyexpense AS select distinct _v customerid ID 作为customerid ID, _o createddatetime AS createddatetime , _o pricegross AS pricegross from (( c_visit _v join c_visit_order _vo on( _vo . visitId = _v . id )) join c_order _o on( _o . id = _vo . orderId ));

这是我的实体:

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Immutable
@Table(name="v_monthlyexpense")
public class View_MonthlyExpense {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="customerid", nullable = false, unique = true)
    private Long customerId;

    @Column(name="createddatetime")
    private Timestamp createdDateTime;

    @Column(name="pricegross")
    private float priceGross;

    protected View_MonthlyExpense() {
    }

    public View_MonthlyExpense(Long customerId, Timestamp createdDateTime, float priceGross) {
        this.customerId = customerId;
        this.createdDateTime = createdDateTime;
        this.priceGross = priceGross;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public Timestamp getCreatedDateTime() {
        return createdDateTime;
    }

    public float getPriceGross() {
        return priceGross;
    }

    @Override
    public String toString() {
        return customerId + ". " + createdDateTime + " - " + priceGross + " USD";
    }
}

这是我的存储库:

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface ExpenseRepository2 extends CrudRepository<View_MonthlyExpense, Long> {
    @Query("SELECT DISTINCT e FROM View_MonthlyExpense e")
    public List<View_MonthlyExpense> findAll();
}

这是我的应用程序:

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExpenseApp implements CommandLineRunner {

    @Autowired
    ExpenseRepository2 repository2;

    public static void main(String[] args) {
        SpringApplication.run(ExpenseApp.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        List<View_MonthlyExpense> list =  repository2.findAll();

        List<View_MonthlyExpense> iter = repository2.findAll();
        iter.forEach(item -> System.out.print(item + "\n"));
    }
}

这是App的output:

  1. 2021-11-24 22:38:55.0 - 8.0 美元
  2. 2021-11-24 22:38:55.0 - 8.0 美元
  3. 2021-11-24 22:38:55.0 - 8.0 美元
  4. 2021-11-24 22:38:55.0 - 8.0 美元
  5. 2021-11-24 22:38:55.0 - 8.0 美元
  6. 2021-11-24 22:38:55.0 - 8.0 美元
  7. 2021-11-24 22:38:55.0 - 8.0 美元
  8. 2021-11-26 23:17:03.0 - 12.0 美元
  9. 2021-11-26 23:17:03.0 - 12.0 美元
  10. 2021-11-26 23:17:03.0 - 12.0 美元
  11. 2021-11-26 23:17:03.0 - 12.0 美元
  12. 2021-11-26 23:17:03.0 - 12.0 美元
  13. 2021-11-26 23:17:03.0 - 12.0 美元
  14. 2021-11-26 23:17:03.0 - 12.0 美元
  15. 2021-11-26 23:17:03.0 - 12.0 美元
  16. 2021-11-26 23:17:03.0 - 12.0 美元

最后,这是数据库中的值:

customerId  createdDateTime     priceGross
1002        24.11.2021 22:38    8
1002        24.11.2021 22:38    10
1002        24.11.2021 22:38    6
1002        25.11.2021 00:46    6
1002        25.11.2021 00:46    10
1002        25.11.2021 00:46    11
1002        25.11.2021 00:46    15
1004        26.11.2021 23:17    12
1004        26.11.2021 23:17    8
1004        26.11.2021 23:17    10
1004        27.11.2021 02:06    12
1004        27.11.2021 02:06    8
1004        27.11.2021 02:06    6
1004        27.11.2021 02:06    30
1004        27.11.2021 02:06    15
1004        27.11.2021 02:06    50

在这里,我将结果的值发布到我的调试器中,这是在我使用 System.out.println 之前:

iter = {ArrayList@7390}  size = 16
 0 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 1 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 2 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 3 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 4 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 5 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 6 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 7 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 8 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 9 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 10 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 11 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 12 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 13 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 14 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 15 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"

提前谢谢你的帮助。

如果要使用迭代器,则必须为每一行调用 a.hasNext() 或 .next()。

while(it.hasNext()) {
     System.out.println(it.next());
}

在这里,我将结果的值发布到我的调试器中,这是在我使用 System.out.println 之前:

iter = {ArrayList@7390}  size = 16
 0 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 1 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 2 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 3 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 4 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 5 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 6 = {View_MonthlyExpense@7395} "1002. 2021-11-24 22:38:55.0 - 8.0 USD"
 7 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 8 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 9 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 10 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 11 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 12 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 13 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 14 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"
 15 = {View_MonthlyExpense@7396} "1004. 2021-11-26 23:17:03.0 - 12.0 USD"

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM