-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Custom types and native queries
PostPosted: Thu Oct 04, 2012 7:28 pm 
Newbie

Joined: Tue Jul 05, 2011 7:28 am
Posts: 10
Hi all,

The application I am working on is using Hibernate 3.5.6-Final. The issue I am having is that named native queries do not seem to use the custom type converter I have setup to ensure that all Date's are UTC/GMT.

Code:

@Entity
@Table(name = "start_end_date")
@NamedNativeQueries({
    @NamedNativeQuery(
        name = "StartEndDate.findAll",
        query = "SELECT start_date AS startDate, end_date AS endDate FROM start_end_date",
        resultSetMapping = "StartEndDate.findAllMapping")
    })

@SqlResultSetMappings({
    @SqlResultSetMapping(
        name = "StartEndDate.findAllMapping",
        entities = {
                @EntityResult(
                        entityClass = StartEndDate.class,
                        fields = {
                            @FieldResult(name = "startDate", column = "startDate"),
                            @FieldResult(name = "endDate", column = "endDate")
                        })
        })
})
public class StartEndDate {

    private Long id;

    private Date startDate;

    private Date endDate;

    @Id
    public Long getId() {
        return id;
    }

    public void setId(final Long entityId) {
        id = entityId;
    }

    @Type(name = "utcTimestamp")
    @Column(name = "start_date")
    @Temporal(TemporalType.DATE)
    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(final Date date) {
        startDate = date;
    }

    @Type(name = "utcTimestamp")
    @Column(name = "end_date")
    @Temporal(TemporalType.DATE)
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(final Date date) {
        endDate = date;
    }

}



If I use the JPA EntityManager to read the StartEndDate entity then everything is okay. However if I use the NamedNativeQuery then the start and end dates that are returned are basically the date/time in the DB with the machines timezone which isn't correct i.e. if the startDate DB DATE is "2012-10-05 14:00:00" and the machine that is running the application is using the EST timezone then using the NamedNativeQuery, the getStartDate method will return "2012-10-05 14:00:00 EST". I have added transient methods to the StartEndDate entity to basically take into account the offset between the machines timezone and UTC/GMT for this scenario but is there a better way to do this with Hibernate 3.5? I presume using Hibernate 3.6 registerHibernateType at bootstrap time would work if I upgraded to this version?

Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.