简体   繁体   中英

Hibernate SQL select result has an entry that doesn't exist in the database

I have a test class where at one point It is supposed to do the following.

  1. Create a report and insert it into the database
  2. Get the RecId number of the highest id (the newly added entry)
  3. return the id
  4. in a another method, get entry with specified id and do stuff with it

On step 4 I get a nullPointerException. Upon debugging and checking the database I realize that, right after running the query no new entries have been made, the entry with the highest ID is "13699". Then it returns ID, uniqueResult, "15718" as if an new entry has actually been made, returns it. The other method tries to get an a report with ID of "15718" and because it doesnt exist, it throws an error.

I am using hibernate so one would expect that the behavior might be different that just SQL but I cant figure out what is going on. Running the same query in mySQL retrieves a different ID.

How does this happen?

Pasting a method that is responsible for steps 1-3

public Integer testInsertReportMYSQL(Integer recipeId) {
    Date yesterday = new Date();
    yesterday.setTime(yesterday.getTime()-(1000*60*24*60));
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String queryString = "INSERT INTO `reports` ( `Recipe_RecID`, `ChargeNo`, `OrderID`, `ANo`, `ANoName`, `BNo`, `BNoName`, `CNo`, `CNoName`, "
            + "`MixerNo`, `DisPointNo`, `State`, `OrderTime`, `ProdTime_1`, `ProdTime_2`, `ProdTime_3`, `ProdTime_4`, `ProdTime_5`, `MixingTime`, "
            + "`NoOfMatLines`, `NoOfRecipeLines`, `SetpointTotalKG`, `SetpointTotalWater`, `SetpointTotalWaterQuick`, `SetpointWaterQuickRecipe`, "
            + "`CondMoistActualQuick`, `ActualTotalKG`, `ActualWaterTotal`, `WashingWater`, `WaterAdjust`, `SetpointTotalM3`, `ActualTotalM3`, "
            + "`ActualWC`, `ActualWP`, `Price`, `Density`, `ReductionSetpoint`, `ReductionActual`, `LastMatTime`, `Temperature`, `YieldStress`, "
            + "`SetpointWC`, `SetpointWP`, `StartTimeInMixer`, `WaterAdjDisPoint`, `WaterAdjDisPointPct`, `CalculatedMoistureScale`, `Comment`, "
            + "`Sample`, `ANoChar`, `BNoChar`, `DisPointName`, `Discarded`, `UtCorrection`) VALUES ( "+recipeId+", 11, 11, 0, 'A-Number', 0, 'OrderNo', 2429, "
            + "'Foelgese', 1, 11, 0, '"+sdf.format(yesterday)+"', 131, 272, 354, 358, 399, 86, 6, 13, 500, 49.2497, 0, 49.2497, 6, 490.467, 39.73, 0, 0, "
            + "0.327682, 0.123456, 2.29123, 2.29123, 45.1681, 1525.87, 0, 0, 359, 0, 0, 2.83807, 2.83807, '1970-01-01 00:00:00', 0, 1, 1.10181, "
            + "NULL, NULL, NULL, NULL, NULL, NULL, NULL);";
    Session session = getSession();
    session.createSQLQuery(queryString).executeUpdate();
    Integer uniqueResult = (Integer) session.createSQLQuery("select recId from Reports order by recId desc limit 1").uniqueResult();
//  session.commit();
    return uniqueResult;
}

I solved it. Turns out the commit in hibernate happens when you close the session. There was a method

private void closeSession(Session session) {
    if (newSessionFactory != null) session.close();
}

that was used elsewhere and then I simply added a line

closeSession(session);

to the testInsertReportMYSQL method before returning the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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