-2

我在 AliasToBeanResultTransformer 的帮助下在 Hibernate 中使用本机 sql 查询...如下所示

        List<took> uy = (List<took>) session
.createSQLQuery("SELECT distinct u.name, cast('Not Defined' as VARCHAR2(6)) as  BOA_Type, u.updated_by, u.updateddate FROM took u where u.FLAG='N' order by u.name")
        .addScalar("name",Hibernate.STRING)
        .addScalar("Access_Type",Hibernate.STRING)
        .addScalar("updated_by",Hibernate.STRING)
        .addScalar("updateddate",Hibernate.TIMESTAMP)
        .setResultTransformer(new AliasToBeanResultTransformer(took.class)).list();

现在问题addScalar("updateddate",Hibernate.TIMESTAMP)出在检查时我发现列表中的所有对象都具有此值

2011-01-03 10:10:55.591

但我希望这个属性的值应该是这种格式..

20110817 17:12

(SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm");)

现在请告知我如何在查询中设置这种格式,以便以后我不必在代码中的任何其他地方显式地进行更改,并且我的 pojo 具有时间戳的这个属性请告知

4

2 回答 2

1

试试这个甲骨文

select to_char(sysdate,'yyyyMMdd HH:mi') from dual

在您的查询中使用它

select to_char(yourdatetimefieldname,'yyyyMMdd HH:mi') from yourtablename

对于Mysql,你可以这样

SELECT date_format(mydatefield,'%Y%m%d %H%i') as mydatefield FROM mytable
于 2013-08-18T06:41:51.010 回答
1

您可以修改查询以在 Oracle 中读取:

SELECT 
  distinct u.name, 
  cast('Not Defined' as VARCHAR2(6)) as BOA_Type, 
  u.updated_by,
  to_char(u.updateddate, 'yyyymmdd hh24:mi') as updated
FROM took u 
where 
  u.FLAG='N' 
order by 
  u.name

http://sqlfiddle.com/#!4/9fef46/2

在 MySQL 中,您的查询将如下所示:

SELECT 
  distinct u.name, 
  cast('Not Defined' as VARCHAR2(6)) as BOA_Type, 
  u.updated_by,
  date_format(u.updateddate, '%Y%m%d %H%i') as updated
FROM took u 
where 
  u.FLAG='N' 
order by 
  u.name

http://sqlfiddle.com/#!2/88fb25/1

于 2013-08-18T06:48:00.193 回答