/
GenericOffsetDateTimeJsonTest.java
156 lines (123 loc) · 4.05 KB
/
GenericOffsetDateTimeJsonTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.vladmihalcea.hibernate.type.json.generic;
import com.vladmihalcea.hibernate.type.json.JsonType;
import com.vladmihalcea.hibernate.util.AbstractPostgreSQLIntegrationTest;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.Type;
import org.junit.Test;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Vlad Mihalcea
*/
public class GenericOffsetDateTimeJsonTest extends AbstractPostgreSQLIntegrationTest {
@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
};
}
@Test
public void test() {
OffsetDateTime dateTime = OffsetDateTime.of(2015, 10, 1, 9, 0 , 0, 0, ZoneOffset.ofHours(2));
doInJPA(entityManager -> {
Location location = new Location();
location.setCountry("Romania");
location.setCity("Cluj-Napoca");
location.setRentedAt(dateTime);
Event event = new Event();
event.setId(1L);
event.setLocation(location);
entityManager.persist(event);
});
doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, 1L);
assertEquals(dateTime, event.getLocation().getRentedAt());
});
}
@Test
public void testWithDouble() {
OffsetDateTime dateTime = OffsetDateTime.of(2022, 7, 14, 11, 37, 0, 0, ZoneOffset.ofHours(0));
//2022-07-14T11:37Z
String dateAsDouble = "1657798620.121624";
String json = String.format("{\n" +
"\"country\":\"Romania\",\n" +
"\"city\":\"Cluj-Napoca\",\n" +
"\"rentedAt\": %s\n" +
"}", dateAsDouble);
doInJDBC(connection -> {
String sql = "INSERT INTO EVENT (ID, LOCATION) VALUES (1, ?::JSON)";
try (
PreparedStatement statement = connection.prepareStatement(sql);
) {
statement.setObject(1, json);
statement.executeUpdate();
} catch (SQLException e) {
fail(e.getMessage());
}
});
doInJPA(entityManager -> {
Event event = entityManager.find(Event.class, 1L);
assertEquals(dateTime, event.getLocation().getRentedAt());
});
}
@Entity(name = "Event")
@Table(name = "event")
public static class Event {
@Id
private Long id;
@Type(JsonType.class)
@Column(columnDefinition = "jsonb")
private Location location;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
public static class Location implements Serializable {
private String country;
private String city;
private BigDecimal reference;
private OffsetDateTime rentedAt;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public BigDecimal getReference() {
return reference;
}
public void setReference(BigDecimal reference) {
this.reference = reference;
}
public OffsetDateTime getRentedAt() {
return rentedAt;
}
public void setRentedAt(OffsetDateTime rentedAt) {
this.rentedAt = rentedAt;
}
}
}