Envrionment:Java,Hibernate JPA提供程序,JPA,Oracle DB
我正在尝试创建一个本机查询来从Oracle DB中获取数据。其中一列是日期列。提取日期,但时间戳显示为00:00:00。但是,通过SQL浏览器,我可以看到日期和时间。如何获取resultList以及时间?
org.hibernate.Query query = ((HibernateEntityManager) entityManager).getSession().
createSQLQuery(NATIVE_QUERY).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP);
query.setString(0, accountNumber);;
query.setTimestamp(1, startDate).setTimestamp(2, endTime);
query.setTimestamp(3, startDate).setTimestamp(4,endTime);
List resultList = query.list();
查询本身(在上面的代码中 - NATIVE_QUERY)是一个普通的SQL查询 - “获取给定日期范围内的所有用户名和txn时间,无论是新事务还是以某种形式更改的事物” 。
选择u.first_name作为firstName,u.last_name作为lastName,tx.start_time作为txnTime来自用户u,事务tx,其中tx.user_id = u.user_id和tx.account_number =?和(tx.start_time>?和tx.start_time<?或tx.change_time>?和tx.change_time<?)
for(Object listItem : resultList){
Map map = (Map) listItem;
Date txnTime = (Date)map.get("txnTime".toUpperCase());
//--------> NOT FETCHING THE TIME. I get the time as 00:00:00
}
域模型非常简单
@Entity
@Table(name="TRANSACTION")
@org.hibernate.annotations.Entity(
dynamicUpdate = true
)
public class Transaction{
@Id
@SequenceGenerator(name="txn_gen", sequenceName="TXN_S")
@GeneratedValue(generator="txn_gen")
@Column(name="TXN_ID")
private Long txnId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USER_ID", nullable=false)
private User user;
@Column(name="START_TIME", nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date startTime;
@Column(name="CHANGE_TIME")
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date changeTime;
}
@Entity
@Table(name="USER")
@org.hibernate.annotations.Entity(
dynamicUpdate = true
)
public class User {
@Id
@SequenceGenerator(name = "user_gen", sequenceName = "USER_S")
@GeneratedValue(generator = "user_gen")
@Column(name="USER_ID")
private Long userId;
@Column(length=500, name="FIRST_NAME")
private String firstName;
@Column(length=500, name="LAST_NAME")
private String lastName;
}