You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JPA将存储过程输出的DateTime读取到Java对象中?

Solution for Mapping SQL Server DATETIME Output to Java Object via JPA Stored Procedure

Let's walk through fixing this issue step by step—your problem boils down to matching the correct Java type to SQL Server's DATETIME to avoid losing time data or hitting mapping errors.

1. Spot the Type Mismatch

Your current code uses java.sql.Date for the timestamp output parameter, but there's a critical mismatch here:

  • java.sql.Date only maps to SQL DATE types (which store date without time)
  • SQL Server's DATETIME includes both date and time components, so you need a type that preserves both. The best options are:
    • java.sql.Timestamp (legacy Java SQL type, works with all JPA providers)
    • java.time.LocalDateTime (Java 8+ modern time API, supported by Hibernate 5.2+ and newer JPA implementations)

2. Update the JPA Stored Procedure Code

Here's the fixed implementation using java.sql.Timestamp first (compatible with most setups):

import java.sql.Timestamp;
import java.time.LocalDateTime;
// ... other necessary imports

// Configure the stored procedure query
StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("dbo.GetErrorandTimestampDetails")
    .registerStoredProcedureParameter("IncidentID", Integer.class, ParameterMode.IN)
    .registerStoredProcedureParameter("Errorcount", Integer.class, ParameterMode.OUT)
    // Replace Date.class with Timestamp.class to capture full date+time
    .registerStoredProcedureParameter("timestamp", Timestamp.class, ParameterMode.OUT)
    .setParameter("IncidentID", incidentID);

// Execute the procedure
boolean executionStatus = storedProcedure.execute();

// Retrieve output values
Integer errorCount = (Integer) storedProcedure.getOutputParameterValue("Errorcount");
Timestamp sqlTimestamp = (Timestamp) storedProcedure.getOutputParameterValue("timestamp");

// Optional: Convert to modern LocalDateTime for easier handling
LocalDateTime incidentTimestamp = sqlTimestamp.toLocalDateTime();

If you're using Java 8+ and a modern JPA provider (like Hibernate 5.2+), you can use LocalDateTime directly without conversion:

import java.time.LocalDateTime;
// ... other necessary imports

StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("dbo.GetErrorandTimestampDetails")
    .registerStoredProcedureParameter("IncidentID", Integer.class, ParameterMode.IN)
    .registerStoredProcedureParameter("Errorcount", Integer.class, ParameterMode.OUT)
    // Use LocalDateTime.class directly for the output parameter
    .registerStoredProcedureParameter("timestamp", LocalDateTime.class, ParameterMode.OUT)
    .setParameter("IncidentID", incidentID);

storedProcedure.execute();

Integer errorCount = (Integer) storedProcedure.getOutputParameterValue("Errorcount");
LocalDateTime incidentTimestamp = (LocalDateTime) storedProcedure.getOutputParameterValue("timestamp");

3. Quick Check on the Stored Procedure

Your existing SQL Server stored procedure is correctly pulling the DATETIME value from Incident_Info.[Date Added]—no changes needed there.

Key Takeaways

  • Always align SQL types with their correct Java counterparts to prevent data loss (like truncated time) or runtime mapping exceptions.
  • If you're on Java 8+, stick to the java.time API classes (like LocalDateTime) for cleaner, more maintainable code compared to legacy java.sql types.

内容的提问来源于stack exchange,提问作者Prateek Narendra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:13:47