|
| 1 | +package com.vladmihalcea.book.hpjp.hibernate.query.function; |
| 2 | + |
| 3 | +import com.vladmihalcea.book.hpjp.util.AbstractPostgreSQLIntegrationTest; |
| 4 | +import org.hibernate.boot.MetadataBuilder; |
| 5 | +import org.hibernate.boot.spi.MetadataBuilderContributor; |
| 6 | +import org.hibernate.dialect.function.SQLFunctionTemplate; |
| 7 | +import org.hibernate.type.StandardBasicTypes; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import javax.persistence.*; |
| 11 | +import java.sql.Timestamp; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.util.Properties; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Vlad Mihalcea |
| 19 | + */ |
| 20 | +public class DateTruncTimeZoneFunctionTest extends AbstractPostgreSQLIntegrationTest { |
| 21 | + |
| 22 | + @Override |
| 23 | + protected Class<?>[] entities() { |
| 24 | + return new Class<?>[]{ |
| 25 | + Post.class, |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void additionalProperties(Properties properties) { |
| 31 | + properties.put( |
| 32 | + "hibernate.metadata_builder_contributor", |
| 33 | + SqlFunctionsMetadataBuilderContributor.class |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public static class SqlFunctionsMetadataBuilderContributor |
| 38 | + implements MetadataBuilderContributor { |
| 39 | + |
| 40 | + @Override |
| 41 | + public void contribute(MetadataBuilder metadataBuilder) { |
| 42 | + metadataBuilder.applySqlFunction( |
| 43 | + "date_trunc", |
| 44 | + new SQLFunctionTemplate( |
| 45 | + StandardBasicTypes.TIMESTAMP, |
| 46 | + "date_trunc('day', (?1 AT TIME ZONE ?2))" |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void afterInit() { |
| 54 | + doInJPA(entityManager -> { |
| 55 | + |
| 56 | + Post post = new Post(); |
| 57 | + post.setId(1L); |
| 58 | + post.setTitle("High-Performance Java Persistence"); |
| 59 | + post.setCreatedOn(Timestamp.valueOf(LocalDateTime.of(2018, 11, 23, 11, 22, 33))); |
| 60 | + |
| 61 | + entityManager.persist(post); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void test() { |
| 67 | + doInJPA(entityManager -> { |
| 68 | + Tuple tuple = entityManager |
| 69 | + .createQuery( |
| 70 | + "select p.title as title, date_trunc(p.createdOn, :timezone) as creation_date " + |
| 71 | + "from Post p " + |
| 72 | + "where p.id = :postId", Tuple.class) |
| 73 | + .setParameter("postId", 1L) |
| 74 | + .setParameter("timezone", "UTC") |
| 75 | + .getSingleResult(); |
| 76 | + |
| 77 | + assertEquals("High-Performance Java Persistence", tuple.get("title")); |
| 78 | + assertEquals(Timestamp.valueOf(LocalDateTime.of(2018, 11, 23, 0, 0, 0)), tuple.get("creation_date")); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @Entity(name = "Post") |
| 83 | + @Table(name = "post") |
| 84 | + public static class Post { |
| 85 | + |
| 86 | + @Id |
| 87 | + private Long id; |
| 88 | + |
| 89 | + private String title; |
| 90 | + |
| 91 | + @Column(name = "created_on") |
| 92 | + private Timestamp createdOn; |
| 93 | + |
| 94 | + public Long getId() { |
| 95 | + return id; |
| 96 | + } |
| 97 | + |
| 98 | + public void setId(Long id) { |
| 99 | + this.id = id; |
| 100 | + } |
| 101 | + |
| 102 | + public String getTitle() { |
| 103 | + return title; |
| 104 | + } |
| 105 | + |
| 106 | + public void setTitle(String title) { |
| 107 | + this.title = title; |
| 108 | + } |
| 109 | + |
| 110 | + public Timestamp getCreatedOn() { |
| 111 | + return createdOn; |
| 112 | + } |
| 113 | + |
| 114 | + public void setCreatedOn(Timestamp createdOn) { |
| 115 | + this.createdOn = createdOn; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments