【发布时间】:2014-09-12 18:06:01
【问题描述】:
注意:我知道这里有很多代码,但您可能只需要查看前 2 个代码块。
我有一个查询,根据该查询的结果,我构建了一个 ArrayList 的 Station 对象 ArrayList<Station>。
我想使用 JPA,因为从长远来看它更灵活,所以我正在调整我的代码以在 JPA 上运行。
所以我给一个方法提供了一个 Station 对象列表,如果该站满足特定的计算标准,这个方法将做一些计算并做station.setPingPong();,与问题无关。
问题是 JPA 的列表源自 3871 PingPongs,而 JDBC 的列表源自 3888 PingPongs,正确的是 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);
这是来自我的Databse 对象的getStationData 方法:
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 不同,构建方式不同,但这没有问题,因为 detectPingPong 方法中没有使用 ID。
以防万一,这是我的 detectPingPong 方法:
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) 相同,并且我得到不同的结果,我假设错误在列表上,因此列表中的对象导致对象的构造。
【问题讨论】:
-
您应该使用
PreparedStatements而不是将参数连接到 SQL 字符串中。
标签: java mysql hibernate jpa jdbc