简体   繁体   English

如何确保使用 Spring JPA 以正确的顺序插入 SQL 数据?

[英]How do I make sure that I insert SQL data in right order with Spring JPA?

When I insert:当我插入时:

data1
data2
data3
data4
data5
..
..
..
dataN

Into a MySQL database with this code:使用以下代码进入 MySQL 数据库:

DataLogg dataLogg = new DataLogg(0, time, DO0, DO1, DO2, DO3, AI0, AI1, AI2, AI3, loggerIdValue, samplingTimeValue, pulseNumber, ControlView.selectedBreakPulseLimit, stopSignal, comment);
dataLoggRepository.saveAndFlush(dataLogg);

It will insert data like this, if I have bad luck.如果我运气不好,它会插入这样的数据。

data1
data2
data4
data3
dataN
..
..
..
data5
    

Question:题:

How can I guarantee that the same order I insert the rows, I can read them as the same order as well?我如何保证插入行的顺序相同,我也可以以相同的顺序读取它们? I don't want it in random order.我不希望它按随机顺序排列。 The last data need to be at the top and the first data need to be at the bottom.最后一个数据需要在顶部,第一个数据需要在底部。

You asked:你问:

How can I guarantee that the same order I insert the rows, I can read them as the same order as well?我如何保证插入行的顺序相同,我也可以以相同的顺序读取它们?

The answer is that there is no internal "order" to a SQL table, for all intents and purposes.答案是,出于所有意图和目的,SQL 表没有内部“顺序”。 The way you ensure a certain order when querying is to use an ORDER BY clause with the query, eg查询时确保特定顺序的方法是在查询中使用ORDER BY子句,例如

SELECT name
FROM yourTable
ORDER BY name;

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

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