Java持久性-无法从oracle表列读取时间
我正在使用持久性和hql将日期从oracle数据库读入java代码。但是,小时、分钟和秒的值始终设置为00 这是我的密码:Java持久性-无法从oracle表列读取时间,java,jpa,timestamp,Java,Jpa,Timestamp,我正在使用持久性和hql将日期从oracle数据库读入java代码。但是,小时、分钟和秒的值始终设置为00 这是我的密码: ... EntityManagerFactory emf = Persistence.createEntityManagerFactory("HotelskoResenjeWSPersistence"); EntityManager em = emf.createEntityManager(); // what i read
...
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HotelskoResenjeWSPersistence");
EntityManager em = emf.createEntityManager();
// what i read from the db is stored here :
List<PpaDrCdr> resultSet = null;
String queryString = "select "
+"id, to_timestamp(substr(chargestart, 1, 17), 'dd.mm.rr hh24:mi:ss') as chargestart "
+"from myoracle@database t "
+"where t.answertime is not null "
+"and t.reportedduration > 0 "
+"and t.directorynumber = :mdn "
+"order by servicestart";
try{
Query query = em.createNativeQuery(queryString, PpaDrCdr.class);
resultSet = query
.setParameter("mdn", account.getMdn())
.getResultList();
}catch(HibernateException ex){
// processing exception here
ex.printStackTrace();
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
if(resultSet != null && !resultSet.isEmpty()){
for ( PpaDrCdr row : resultSet ) {
System.out.println("\n\t"+
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(row.getDate1())+
"\n");
}
}
...
为什么坚持不读时间?
提前感谢。使用java.time.LocalDateTime
@Column(name="chargestart")
@Basic
private LocalDateTime chargestart;
它已经是Hibernate中已知的基本数据类型。
(>)
另外,我建议使用Hibernate的CriteriaAPI进行查询。避免使用SQL语句是使用Hibernate的好处之一。Ok。我终于解决了。我放弃了实体类,将数据库中的所有内容作为结果集的
列表加载,然后将单个对象强制转换为我需要的:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPersistence");
EntityManager em = emf.createEntityManager();
List<Object[]> oList = new ArrayList<Object[]>();
try{
Session s1 = (Session) em.getDelegate();
oList = s1.getNamedQuery("MyNamedQueryFromOrmXml")
.setParameter("mdn", account.getMdn())
.list();
}catch(HibernateException ex){
ex.printStackTrace();
ApplicationContext context =
new FileSystemXmlApplicationContext("classpath:beans.xml");
GenericFaultType gft = ((HibernateExceptionToGFTConverter)context.getBean("GFTbean")).convert(ex);
throw new GetCDREntryListFaultMsg("HibernateException occurred", gft, ex);
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
// this is what i will need :
List<CDREntry> cdrE = new ArrayList<CDREntry>();
for(Object[] o : oList){
if(o!=null){
try{
CDREntry pom = new CDREntry();
pom.setId((long)o[0]);
// AND NOW I GET TIME TOO !!!
pom.setChargingStart((Timestamp)o[1]);
//...
cdrE.add(pom);
}catch(Exception e){
System.out.print("\n\tFailed to cast "+e.getMessage());
}
}
}
EntityManagerFactory emf=Persistence.createEntityManagerFactory(“MyPersistence”);
EntityManager em=emf.createEntityManager();
List-oList=new-ArrayList();
试一试{
会话s1=(会话)em.getDelegate();
oList=s1.getNamedQuery(“MyNamedQueryFromOrmXml”)
.setParameter(“mdn”,account.getMdn())
.list();
}捕获(HibernateeException例外){
例如printStackTrace();
ApplicationContext上下文=
新的FileSystemXmlApplicationContext(“classpath:beans.xml”);
GenericFaultType gft=((HibernateExceptionToGFTConverter)context.getBean(“GFTbean”).convert(ex);
抛出新的GetCDREntryListFaultMsg(“HibernateeException发生”,gft,ex);
}捕获(例外e){
e、 printStackTrace();
抛出新的运行时异常(e);
}
//这就是我需要的:
List cdrE=new ArrayList();
for(对象[]o:oList){
如果(o!=null){
试一试{
cdrrentry pom=新的cdrrentry();
pom.setId((长)o[0]);
//现在我也有时间了!!!
pom.setChargingStart((时间戳)o[1]);
//...
cdrE.add(pom);
}捕获(例外e){
System.out.print(“\n\t未能转换”+e.getMessage());
}
}
}
不知道为什么旧版本不起作用,但下面是它现在的工作方式,使用session和orm.xml您确定数据库中有完整的日期和时间吗?另外,您使用的是本机SQL查询,而不是HQL。在使用JPAAPI时,您究竟为什么要硬编码HibernateException?!JPAAPI保证JPA异常,然后您的代码是可移植的。。。嗯,由于您使用的是hacky SQL,所以它不是可移植的数据存储,但这是另一种情况,我必须使用查询,因为表有23列,我只需要特定的值。尽管如此,我还是会浏览你发布的教程。也许我会想些什么。问题是,从databsae读取的每个时间戳都被重置为午夜(00:00:00),介于我的查询和我的类变量的设置之间,我不知道为什么。@Basic注释是可选的,因为Hibernate已经知道LocalDateTime。重置的日期听起来很奇怪。我想这与数据库中的SQL类型和Java类型之间的映射有关。我的同事告诉我,java.util.Date和java.sql.Date不应该被使用,因为它们经常会导致数据库类型转换方面的问题。这就是我们使用LocalDateTime的原因。
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPersistence");
EntityManager em = emf.createEntityManager();
List<Object[]> oList = new ArrayList<Object[]>();
try{
Session s1 = (Session) em.getDelegate();
oList = s1.getNamedQuery("MyNamedQueryFromOrmXml")
.setParameter("mdn", account.getMdn())
.list();
}catch(HibernateException ex){
ex.printStackTrace();
ApplicationContext context =
new FileSystemXmlApplicationContext("classpath:beans.xml");
GenericFaultType gft = ((HibernateExceptionToGFTConverter)context.getBean("GFTbean")).convert(ex);
throw new GetCDREntryListFaultMsg("HibernateException occurred", gft, ex);
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
// this is what i will need :
List<CDREntry> cdrE = new ArrayList<CDREntry>();
for(Object[] o : oList){
if(o!=null){
try{
CDREntry pom = new CDREntry();
pom.setId((long)o[0]);
// AND NOW I GET TIME TOO !!!
pom.setChargingStart((Timestamp)o[1]);
//...
cdrE.add(pom);
}catch(Exception e){
System.out.print("\n\tFailed to cast "+e.getMessage());
}
}
}