gpt4 book ai didi

Java JPA 和 Java JDBC 对于同一 MySQL 查询返回不同的结果

转载 作者:行者123 更新时间:2023-11-30 00:02:49 25 4
gpt4 key购买 nike

注意:我知道这里有很多代码,但您可能只需要查看前 2 个代码块。

我有一个查询,根据该查询的结果,我构建了一个 ArrayListStation对象,ArrayList<Station> .

我想使用 JPA,因为从长远来看它使用起来更灵活,因此我正在调整我的代码以在 JPA 上运行。

因此,我将 Station 对象的列表提供给一个方法,该方法将进行一些计算并执行 station.setPingPong();如果该电台符合某些计算标准,则与问题无关。

问题是来自 JPA 的列表源自 3871 个 PingPong,而来自 JDBC 的列表源自 3888 个 PingPong,正确的列表是 3888。为什么会这样?

这是我使用数据库中的数据构建对象的代码:

for (int i = 0; i < uniqueStations.size(); i++) 
{

ArrayList<Station> stationDataList = new ArrayList<>();
ArrayList<Station> auxStationDataList = new ArrayList<>();
ArrayList<Station> pingPongProccessedData = new ArrayList<>();
ArrayList<Station> pingPongProccessedDataJPA = new ArrayList<>();

// Query using JDBC
String queryToApi = "SELECT * "
+ "FROM " + readTable + " "
+ "WHERE calling_station_id = '" + uniqueStations.get(i) + "' "
+ "order by timestamp-acct_session_time, timestamp;";

// Query using JPA
String query = "SELECT timestamp-acct_session_time, timestamp, called_station_id "
+ "FROM " + readTable + " "
+ "WHERE calling_station_id = '" + uniqueStations.get(i) + "' "
+ "order by timestamp-acct_session_time, timestamp;";

// Using JDBC, here Station objects are created inside getStationData method, inserted into the list and returned.
stationDataList = Database.getInstance().getStationData(api.getConnectionSourceServer(), query, false);

// Using JPA, JavaLogs is my Class Entity from the database (it represents my table and was generated by NetBeans)
List<JavaLogs> javaLogsList = api.executeSelectQuery(queryToApi, JavaLogs.class, "TesteV2PU");
int repetitionSTA = 0;
// Now I'm creating the Station objects
for(JavaLogs javaLogs : javaLogsList)
{
int id = javaLogs.getIdacesso();
long timestamp = javaLogs.getTimestamp();
long accountSessionTime = javaLogs.getAcctSessionTime();
int startTime = (int) (timestamp - accountSessionTime);
int endTime = javaLogs.getTimestamp();
String accessPoint = javaLogs.getCalledStationId();
int ttPrevious = 0;
int ttNext = 0;
int pPong = 0;
Station station = new Station(id, startTime, endTime, accessPoint, stationAnonymized++, repetitionSTA++, ttPrevious, ttNext, pPong);

auxStationDataList.add(station);
}

// The problem comes here, pingPongProccessedData does have the correct computation of Station objects
// But pingPongProccessedDataJPA doesn't have the correct computation of Station objects
// In other words, pingPongProccessedDataJPA is WRONG and pingPongProccessedData is correct.

pingPongProccessedData = detectPingPong(accessThreshold, transitionThreshold, stationDataList);
pingPongProccessedDataJPA = detectPingPong(accessThreshold, transitionThreshold, auxStationDataList);

pingPongStationList.addAll(pingPongProccessedData);
pingPongStationListJPA.addAll(pingPongProccessedDataJPA);

}

int totalPingPongsJDBC = 0;
int totalPingPongsJPA = 0;

for(Station station : pingPongStationList)
{
if(station.getPingPong() == 1)
{
totalPingPongsJDBC++;
}
}

for(Station station : pingPongStationListJPA)
{
if(station.getPingPong() == 1)
{
totalPingPongsJPA++;
}
}

// Here I get in the console: TOTAL PingPongs JPA: 3871 TOTAL PingPongs JDBC: 3888
System.out.println("TOTAL PingPongs JPA: " + totalPingPongsJPA + " TOTAL PingPongs JDBC: " + totalPingPongsJDBC);

这是我的getStationData我的方法Databse对象:

public ArrayList<Station> getStationData(Connection con, String query, boolean isStationAnonymizeRequired) throws SQLException
{
ArrayList<Station> stationList = new ArrayList<>();

Statement s = con.createStatement();
ResultSet rs = s.executeQuery(query);

int repetitionSTA=1;

while (rs.next())
{
/*
* SQL Retorns:
* StartTime - EndTime - AP - STA
*
* Objecto Station:
* ID - StartTime - EndTime - AP - STA - RepetitionSTA - TransitionTimePrevious - TransitionTimeNext - PingPong
*/

if(isStationAnonymizeRequired == false)
{
Station station = new Station(id, rs.getInt(1), rs.getInt(2), rs.getString(3), stationMacAddress, repetitionSTA, 0, 0, 0);
stationList.add(station);
repetitionSTA++;
id++;
}
else
{ // (id, startTime, endTime, ap, sta, repetitionSTA, ttprevious, ttnext, ppong)
Station station = new Station(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4),
rs.getInt(5), repetitionSTA, rs.getInt(6), rs.getInt(7), rs.getInt(8));
stationList.add(station);
repetitionSTA++;
}
}
rs.close();
s.close();
stationMacAddress++;

return stationList;
}

我知道 ID 不同,构建方式不同,但这没有问题,因为 ID 未在 detectPingPong 中使用。方法。

以防万一,这是我的 detectorPingPong 方法:

public ArrayList<Station> detectPingPong(int accessSessionTime, int transitionTime, ArrayList<Station> stationDataList)
{

for(int i = 1; i<stationDataList.size()-1; i++) //i=1 to skip the first position and -1 to skip the last position
{
/*
* StartTime - EndTime - AP - STA - TTprevious - TTnext - PPong - ID
*/

/*
* At current position values
*/
int startTimeActual = stationDataList.get(i).getStartTime();
int endTimeActual = stationDataList.get(i).getEndTime();
String apActual = stationDataList.get(i).getAccessPoint();

/*
* get the values from the previous position
* */
int endTimePrevious = stationDataList.get(i-1).getEndTime();
String apPrevious = stationDataList.get(i-1).getAccessPoint();

/*
* get the values from the next position
* */
int startTimeNext = stationDataList.get(i+1).getStartTime();
String apNext = stationDataList.get(i+1).getAccessPoint();


//Calc transition times:
int transitionTimePrevious = startTimeActual - endTimePrevious;
int transitionTimeNext = startTimeNext - endTimeActual;

stationDataList.get(i).setTransitionTimePrevious(transitionTimePrevious);
stationDataList.get(i).setTransitionTimeNext(transitionTimeNext);


/*
* Testing the conditions to occur a pingpong
* */
if(transitionTimePrevious<0)
{
transitionTimePrevious=0;
stationDataList.get(i).setTransitionTimePrevious(transitionTimePrevious);
}


if(transitionTimeNext<0)
{
transitionTimeNext=0;
stationDataList.get(i).setTransitionTimeNext(transitionTimeNext);
}


/*
* ***************************
* * TEST PING PONG *
* ***************************
*/


if( endTimeActual - startTimeActual < accessSessionTime &&
transitionTimePrevious < transitionTime &&
transitionTimeNext < transitionTime &&
apActual.equalsIgnoreCase(apPrevious) == false &&
stationDataList.get(i).getRepetitionSTA() > 1
)
{
stationDataList.get(i).setPingPong();
}

}
return stationDataList;
}

因此,由于两个列表的方法( detectPingPong )是相同的,并且我得到不同的结果,我假设错误出现在列表上,因此列表中的对象导致了对象的构造。

最佳答案

您正在将结果映射到 bean 类 (javalogs)。可能有一些检索到的数据与 JavaLogs 字段不匹配。那些检索到的行将被忽略。这就是您看到结果之间存在差异的原因。

关于Java JPA 和 Java JDBC 对于同一 MySQL 查询返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com