Skip to content

Commit 56b2799

Browse files
committed
Add test for date_trunc
1 parent ee45168 commit 56b2799

File tree

1 file changed

+120
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)