[英]Spring Boot JPA Query on MySQL reads only the first row
I am facing a strange behaviour when reading a view from mysql.从 mysql 读取视图时,我遇到了一个奇怪的行为。 My select statement is reading only the first row.我的 select 语句只读取第一行。
But it works fine when I read other db-tables.但是当我阅读其他数据库表时它工作正常。
Has someone an idea what it can be?有人知道它可能是什么吗?
This is my create statement:这是我的创建声明:
CREATE ALGORITHM=UNDEFINED DEFINER=
root
@localhost
SQL SECURITY DEFINER VIEWv_monthlyexpense
AS select distinct_v
. CREATE ALGORITHM=UNDEFINED DEFINER=root
@localhost
SQL 安全定义器视图v_monthlyexpense
AS select distinct_v
。customerid
AScustomerid
,_o
.customerid
ID 作为customerid
ID,_o
。createddatetime
AScreateddatetime
,_o
.createddatetime
AScreateddatetime
,_o
。pricegross
ASpricegross
from ((c_visit
_v
joinc_visit_order
_vo
on(_vo
.visitId
=_v
.id
)) joinc_order
_o
on(_o
.id
=_vo
.orderId
));pricegross
ASpricegross
from ((c_visit
_v
joinc_visit_order
_vo
on(_vo
.visitId
=_v
.id
)) joinc_order
_o
on(_o
.id
=_vo
.orderId
));
This is my Entity:这是我的实体:
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";
}
}
This is my repository:这是我的存储库:
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();
}
This is my App:这是我的应用程序:
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"));
}
}
This is the output of the App:这是App的output:
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-24 22:38:55.0 - 8.0 USD 2021-11-24 22:38:55.0 - 8.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
- 2021-11-26 23:17:03.0 - 12.0 USD 2021-11-26 23:17:03.0 - 12.0 美元
And finally, this are the values in the database:最后,这是数据库中的值:
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
Here I post the values of the result in my debuger, this is before I use System.out.println:在这里,我将结果的值发布到我的调试器中,这是在我使用 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"
Thank you for your help in advance.提前谢谢你的帮助。
If you are going to use a Iterator you have to make a.hasNext() or.next() call for each row.如果要使用迭代器,则必须为每一行调用 a.hasNext() 或 .next()。
while(it.hasNext()) {
System.out.println(it.next());
}
Here I post the values of the result in my debuger, this is before I use System.out.println:在这里,我将结果的值发布到我的调试器中,这是在我使用 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.