Skip to content

Commit f3a8a42

Browse files
committed
Add test case to for mapping the Java Month type to a MySQL tinyint column
1 parent 340d847 commit f3a8a42

File tree

3 files changed

+160
-2
lines changed

3 files changed

+160
-2
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.vladmihalcea.hpjp.hibernate.type.attributeconverter;
2+
3+
import com.vladmihalcea.hpjp.util.AbstractMySQLIntegrationTest;
4+
import io.hypersistence.utils.hibernate.type.DescriptorImmutableType;
5+
import io.hypersistence.utils.hibernate.type.basic.Iso8601MonthType;
6+
import io.hypersistence.utils.hibernate.type.basic.YearType;
7+
import io.hypersistence.utils.hibernate.type.basic.internal.Iso8601MonthMonthTypeDescriptor;
8+
import io.hypersistence.utils.hibernate.type.util.Configuration;
9+
import jakarta.persistence.*;
10+
import org.hibernate.HibernateException;
11+
import org.hibernate.Session;
12+
import org.hibernate.annotations.NaturalId;
13+
import org.hibernate.annotations.Type;
14+
import org.hibernate.type.descriptor.WrapperOptions;
15+
import org.hibernate.type.descriptor.java.AbstractClassJavaType;
16+
import org.hibernate.type.descriptor.java.YearJavaType;
17+
import org.hibernate.type.descriptor.jdbc.IntegerJdbcType;
18+
import org.hibernate.type.descriptor.jdbc.JdbcType;
19+
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
20+
import org.hibernate.type.descriptor.jdbc.TinyIntJdbcType;
21+
import org.junit.Test;
22+
23+
import java.sql.Types;
24+
import java.time.Month;
25+
import java.time.Year;
26+
import java.time.YearMonth;
27+
import java.util.Objects;
28+
import java.util.Properties;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* @author Vlad Mihalcea
34+
*/
35+
public class MySQLYearAndMonthIntegerTest extends AbstractMySQLIntegrationTest {
36+
37+
@Override
38+
protected Class<?>[] entities() {
39+
return new Class<?>[]{
40+
Book.class
41+
};
42+
}
43+
44+
@Override
45+
protected void beforeInit() {
46+
executeStatement("drop table if exists book");
47+
executeStatement("create table book (publishing_month tinyint, publishing_year int, id bigint not null auto_increment, isbn varchar(255), title varchar(255), primary key (id)) engine=InnoDB");
48+
executeStatement("alter table book add constraint UK_book_isbn unique (isbn)");
49+
}
50+
51+
@Override
52+
protected void additionalProperties(Properties properties) {
53+
properties.put("hibernate.hbm2ddl.auto", "validate");
54+
}
55+
56+
@Test
57+
public void test() {
58+
doInJPA(entityManager -> {
59+
Book book = new Book();
60+
book.setIsbn("978-9730228236");
61+
book.setTitle("High-Performance Java Persistence");
62+
book.setPublishingYear(Year.of(2016));
63+
book.setPublishingMonth(Month.of(10));
64+
65+
entityManager.persist(book);
66+
});
67+
68+
doInJPA(entityManager -> {
69+
Book book = entityManager
70+
.unwrap(Session.class)
71+
.bySimpleNaturalId(Book.class)
72+
.load("978-9730228236");
73+
74+
assertEquals(Year.of(2016), book.getPublishingYear());
75+
assertEquals(Month.of(10), book.getPublishingMonth());
76+
});
77+
78+
doInJPA(entityManager -> {
79+
Book book = entityManager.createQuery("""
80+
select b
81+
from Book b
82+
where
83+
b.title = :title and
84+
b.publishingYear = :publishingYear and
85+
b.publishingMonth = :publishingMonth
86+
""", Book.class)
87+
.setParameter("title", "High-Performance Java Persistence")
88+
.setParameter("publishingYear", Year.of(2016))
89+
.setParameter("publishingMonth", Month.of(10))
90+
.getSingleResult();
91+
92+
assertEquals("978-9730228236", book.getIsbn());
93+
});
94+
}
95+
96+
97+
@Entity(name = "Book")
98+
@Table(name = "book")
99+
public static class Book {
100+
101+
@Id
102+
@GeneratedValue(strategy = GenerationType.IDENTITY)
103+
private Long id;
104+
105+
@NaturalId
106+
private String isbn;
107+
108+
private String title;
109+
110+
@Column(name = "publishing_year")
111+
@Type(YearType.class)
112+
private Year publishingYear;
113+
114+
@Column(name = "publishing_month", columnDefinition = "tinyint")
115+
@Type(Iso8601MonthType.class)
116+
private Month publishingMonth;
117+
118+
public Long getId() {
119+
return id;
120+
}
121+
122+
public void setId(Long id) {
123+
this.id = id;
124+
}
125+
126+
public String getIsbn() {
127+
return isbn;
128+
}
129+
130+
public void setIsbn(String isbn) {
131+
this.isbn = isbn;
132+
}
133+
134+
public String getTitle() {
135+
return title;
136+
}
137+
138+
public void setTitle(String title) {
139+
this.title = title;
140+
}
141+
142+
public Year getPublishingYear() {
143+
return publishingYear;
144+
}
145+
146+
public void setPublishingYear(Year publishingYear) {
147+
this.publishingYear = publishingYear;
148+
}
149+
150+
public Month getPublishingMonth() {
151+
return publishingMonth;
152+
}
153+
154+
public void setPublishingMonth(Month publishingMonth) {
155+
this.publishingMonth = publishingMonth;
156+
}
157+
}
158+
}

core/src/test/java/com/vladmihalcea/hpjp/hibernate/type/attributeconverter/MySQLYearMonthIntegerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MySQLYearMonthIntegerTest extends AbstractMySQLIntegrationTest {
1818
@Override
1919
protected Class<?>[] entities() {
2020
return new Class<?>[]{
21-
Book.class
21+
Book.class
2222
};
2323
}
2424

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@
536536
<jackson.version>2.14.3</jackson.version>
537537

538538
<flyway.version>6.4.4</flyway.version>
539-
<hypersistence-utils.version>3.8.1</hypersistence-utils.version>
539+
<hypersistence-utils.version>3.9.1</hypersistence-utils.version>
540540
<hypersistence-tsid.version>2.1.2</hypersistence-tsid.version>
541541

542542
<blaze-persistence.version>1.6.13</blaze-persistence.version>

0 commit comments

Comments
 (0)