@@ -0,0 +1,3297 @@
+package com.vladmihalcea.book.hpjp.hibernate.query.function;%0A%0Aimport com.vladmihalcea.book.hpjp.util.AbstractPostgreSQLIntegrationTest;%0Aimport org.hibernate.boot.MetadataBuilder;%0Aimport org.hibernate.boot.spi.MetadataBuilderContributor;%0Aimport org.hibernate.dialect.function.SQLFunctionTemplate;%0Aimport org.hibernate.dialect.function.StandardSQLFunction;%0Aimport org.hibernate.type.StandardBasicTypes;%0Aimport org.junit.Test;%0A%0Aimport javax.persistence.*;%0Aimport java.sql.Timestamp;%0Aimport java.time.LocalDate;%0Aimport java.time.LocalDateTime;%0Aimport java.util.Properties;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0A/**%0A * @author Vlad Mihalcea%0A */%0Apublic class DateTruncUtcFunctionTest extends AbstractPostgreSQLIntegrationTest %7B%0A%0A    @Override%0A    protected Class%3C?%3E%5B%5D entities() %7B%0A        return new Class%3C?%3E%5B%5D%7B%0A                Post.class,%0A        %7D;%0A    %7D%0A%0A    @Override%0A    protected void additionalProperties(Properties properties) %7B%0A        properties.put(%0A                %22hibernate.metadata_builder_contributor%22,%0A                SqlFunctionsMetadataBuilderContributor.class%0A        );%0A    %7D%0A%0A    public static class SqlFunctionsMetadataBuilderContributor%0A            implements MetadataBuilderContributor %7B%0A%0A        @Override%0A        public void contribute(MetadataBuilder metadataBuilder) %7B%0A            metadataBuilder.applySqlFunction(%0A                    %22date_trunc%22,%0A                    new SQLFunctionTemplate(%0A                            StandardBasicTypes.TIMESTAMP,%0A                            %22date_trunc('day', (?1 AT TIME ZONE 'UTC'))%22%0A                    )%0A            );%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void afterInit() %7B%0A        doInJPA(entityManager -%3E %7B%0A%0A            Post post = new Post();%0A            post.setId(1L);%0A            post.setTitle(%22High-Performance Java Persistence%22);%0A            post.setCreatedOn(Timestamp.valueOf(LocalDateTime.of(2018, 11, 23, 11, 22, 33)));%0A%0A            entityManager.persist(post);%0A        %7D);%0A    %7D%0A%0A    @Test%0A    public void test() %7B%0A        doInJPA(entityManager -%3E %7B%0A            Tuple tuple = entityManager%0A            .createQuery(%0A                %22select p.title as title, date_trunc(p.createdOn) as creation_date %22 +%0A                %22from Post p %22 +%0A                %22where p.id = :postId%22, Tuple.class)%0A            .setParameter(%22postId%22, 1L)%0A            .getSingleResult();%0A%0A            assertEquals(%22High-Performance Java Persistence%22, tuple.get(%22title%22));%0A            assertEquals(Timestamp.valueOf(LocalDateTime.of(2018, 11, 23, 0, 0, 0)), tuple.get(%22creation_date%22));%0A        %7D);%0A    %7D%0A%0A    @Entity(name = %22Post%22)%0A    @Table(name = %22post%22)%0A    public static class Post %7B%0A%0A        @Id%0A        private Long id;%0A%0A        private String title;%0A%0A        @Column(name = %22created_on%22)%0A        private Timestamp createdOn;%0A%0A        public Long getId() %7B%0A            return id;%0A        %7D%0A%0A        public void setId(Long id) %7B%0A            this.id = id;%0A        %7D%0A%0A        public String getTitle() %7B%0A            return title;%0A        %7D%0A%0A        public void setTitle(String title) %7B%0A            this.title = title;%0A        %7D%0A%0A        public Timestamp getCreatedOn() %7B%0A            return createdOn;%0A        %7D%0A%0A        public void setCreatedOn(Timestamp createdOn) %7B%0A            this.createdOn = createdOn;%0A        %7D%0A    %7D%0A%0A%7D%0A

Add test for date_trunc