有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate SQL选择结果包含数据库中不存在的条目

我有一个测试类,在某一点上它应该做以下事情

  1. 创建报告并将其插入数据库
  2. 获取最高id的RecId号(新添加的条目)
  3. 返回id
  4. 在另一种方法中,获取具有指定id的条目并使用它进行操作

在第4步,我得到一个nullPointerException。在调试和检查数据库后,我意识到,在运行查询之后,没有新条目出现,ID最高的条目是“13699”。然后它返回ID,uniqueResult,“15718”,就好像一个新条目实际上已经被创建了一样,返回它。另一种方法尝试获取ID为“15718”的a报告,由于该报告不存在,因此会抛出一个错误

我使用的是hibernate,所以人们会认为它的行为可能与SQL有所不同,但我无法理解到底发生了什么。在mySQL中运行相同的查询会检索不同的ID

这是怎么发生的

粘贴负责步骤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;
}

共 (0) 个答案