Skip to content

Commit bd95810

Browse files
committed
YearMonthTypeDescriptor failed to wrap null value from database in use case of YearMonthIntegerType vladmihalcea#113
1 parent b37c4a7 commit bd95810

File tree

9 files changed

+153
-29
lines changed

9 files changed

+153
-29
lines changed

hibernate-types-52/src/main/java/com/vladmihalcea/hibernate/type/basic/internal/YearMonthTypeDescriptor.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ public <X> YearMonth wrap(X value, WrapperOptions options) {
6969
}
7070
if (value instanceof Number) {
7171
int numericValue = ((Number) (value)).intValue();
72-
int year = numericValue / 100;
73-
int month = numericValue % 100;
74-
return YearMonth.of(year, month);
72+
if(numericValue > 0) {
73+
int year = numericValue / 100;
74+
int month = numericValue % 100;
75+
return YearMonth.of(year, month);
76+
} else {
77+
return null;
78+
}
7579
}
7680
if (value instanceof Date) {
7781
Date date = (Date) value;

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/MySQLYearMonthDateTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.TimeZone;
1212

1313
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNull;
1415

1516
/**
1617
* @author Vlad Mihalcea
@@ -74,6 +75,25 @@ public void test() {
7475
});
7576
}
7677

78+
@Test
79+
public void testNull() {
80+
doInJPA(entityManager -> {
81+
Book book = new Book();
82+
book.setIsbn("123-456");
83+
book.setPublishedOn(null);
84+
85+
entityManager.persist(book);
86+
});
87+
88+
doInJPA(entityManager -> {
89+
Book book = entityManager
90+
.unwrap(Session.class)
91+
.bySimpleNaturalId(Book.class)
92+
.load("123-456");
93+
94+
assertNull(book.getPublishedOn());
95+
});
96+
}
7797

7898
@Entity(name = "Book")
7999
@Table(name = "book")

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/MySQLYearMonthIntegerTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.time.YearMonth;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNull;
1314

1415
/**
1516
* @author Vlad Mihalcea
@@ -59,6 +60,25 @@ public void test() {
5960
});
6061
}
6162

63+
@Test
64+
public void testNull() {
65+
doInJPA(entityManager -> {
66+
Book book = new Book();
67+
book.setIsbn("123-456");
68+
book.setPublishedOn(null);
69+
70+
entityManager.persist(book);
71+
});
72+
73+
doInJPA(entityManager -> {
74+
Book book = entityManager
75+
.unwrap(Session.class)
76+
.bySimpleNaturalId(Book.class)
77+
.load("123-456");
78+
79+
assertNull(book.getPublishedOn());
80+
});
81+
}
6282

6383
@Entity(name = "Book")
6484
@Table(name = "book")

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLYearMonthDateTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.time.YearMonth;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertNull;
1314

1415
/**
1516
* @author Vlad Mihalcea
@@ -59,6 +60,25 @@ public void test() {
5960
});
6061
}
6162

63+
@Test
64+
public void testNull() {
65+
doInJPA(entityManager -> {
66+
Book book = new Book();
67+
book.setIsbn("123-456");
68+
book.setPublishedOn(null);
69+
70+
entityManager.persist(book);
71+
});
72+
73+
doInJPA(entityManager -> {
74+
Book book = entityManager
75+
.unwrap(Session.class)
76+
.bySimpleNaturalId(Book.class)
77+
.load("123-456");
78+
79+
assertNull(book.getPublishedOn());
80+
});
81+
}
6282

6383
@Entity(name = "Book")
6484
@Table(name = "book")

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLYearMonthEpochTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.stream.Collectors;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNull;
1920

2021
/**
2122
* @author Vlad Mihalcea
@@ -70,6 +71,26 @@ public void test() {
7071
});
7172
}
7273

74+
@Test
75+
public void testNull() {
76+
doInJPA(entityManager -> {
77+
Book book = new Book();
78+
book.setIsbn("123-456");
79+
book.setPublishedOn(null);
80+
81+
entityManager.persist(book);
82+
});
83+
84+
doInJPA(entityManager -> {
85+
Book book = entityManager
86+
.unwrap(Session.class)
87+
.bySimpleNaturalId(Book.class)
88+
.load("123-456");
89+
90+
assertNull(book.getPublishedOn());
91+
});
92+
}
93+
7394
@Test
7495
@Ignore
7596
public void testIndexing() {

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLYearMonthIntegerTest.java

+31-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.stream.Collectors;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
1921

2022
/**
2123
* @author Vlad Mihalcea
@@ -56,20 +58,40 @@ public void test() {
5658

5759
doInJPA(entityManager -> {
5860
Book book = entityManager
59-
.createQuery(
60-
"select b " +
61-
"from Book b " +
62-
"where " +
63-
" b.title = :title and " +
64-
" b.publishedOn = :publishedOn", Book.class)
65-
.setParameter("title", "High-Performance Java Persistence")
66-
.setParameter("publishedOn", YearMonth.of(2016, 10))
67-
.getSingleResult();
61+
.createQuery(
62+
"select b " +
63+
"from Book b " +
64+
"where " +
65+
" b.title = :title and " +
66+
" b.publishedOn = :publishedOn", Book.class)
67+
.setParameter("title", "High-Performance Java Persistence")
68+
.setParameter("publishedOn", YearMonth.of(2016, 10))
69+
.getSingleResult();
6870

6971
assertEquals("978-9730228236", book.getIsbn());
7072
});
7173
}
7274

75+
@Test
76+
public void testNull() {
77+
doInJPA(entityManager -> {
78+
Book book = new Book();
79+
book.setIsbn("123-456");
80+
book.setPublishedOn(null);
81+
82+
entityManager.persist(book);
83+
});
84+
85+
doInJPA(entityManager -> {
86+
Book book = entityManager
87+
.unwrap(Session.class)
88+
.bySimpleNaturalId(Book.class)
89+
.load("123-456");
90+
91+
assertNull(book.getPublishedOn());
92+
});
93+
}
94+
7395
@Test
7496
@Ignore
7597
public void testIndexing() {

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLYearMonthTimestampTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.time.YearMonth;
1515

1616
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertNull;
1718

1819
/**
1920
* @author Vlad Mihalcea
@@ -63,6 +64,25 @@ public void test() {
6364
});
6465
}
6566

67+
@Test
68+
public void testNull() {
69+
doInJPA(entityManager -> {
70+
Book book = new Book();
71+
book.setIsbn("123-456");
72+
book.setPublishedOn(null);
73+
74+
entityManager.persist(book);
75+
});
76+
77+
doInJPA(entityManager -> {
78+
Book book = entityManager
79+
.unwrap(Session.class)
80+
.bySimpleNaturalId(Book.class)
81+
.load("123-456");
82+
83+
assertNull(book.getPublishedOn());
84+
});
85+
}
6686

6787
@Entity(name = "Book")
6888
@Table(name = "book")

hibernate-types-52/src/test/java/com/vladmihalcea/hibernate/type/basic/YearAndMonthTest.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.vladmihalcea.hibernate.type.basic;
22

33
import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest;
4-
import com.vladmihalcea.hibernate.type.util.AbstractPostgreSQLIntegrationTest;
54
import org.hibernate.Session;
65
import org.hibernate.annotations.NaturalId;
76
import org.hibernate.annotations.TypeDef;
@@ -10,7 +9,6 @@
109
import javax.persistence.*;
1110
import java.time.Month;
1211
import java.time.Year;
13-
import java.time.YearMonth;
1412

1513
import static org.junit.Assert.assertEquals;
1614

@@ -22,7 +20,7 @@ public class YearAndMonthTest extends AbstractMySQLIntegrationTest {
2220
@Override
2321
protected Class<?>[] entities() {
2422
return new Class<?>[]{
25-
Publisher.class
23+
Publisher.class
2624
};
2725
}
2826

@@ -39,31 +37,30 @@ public void test() {
3937

4038
doInJPA(entityManager -> {
4139
Publisher publisher = entityManager
42-
.unwrap(Session.class)
43-
.bySimpleNaturalId(Publisher.class)
44-
.load("vladmihalcea.com");
40+
.unwrap(Session.class)
41+
.bySimpleNaturalId(Publisher.class)
42+
.load("vladmihalcea.com");
4543

4644
assertEquals(Year.of(2013), publisher.getEstYear());
4745
assertEquals(Month.NOVEMBER, publisher.getSalesMonth());
4846
});
4947

5048
doInJPA(entityManager -> {
5149
Publisher book = entityManager
52-
.createQuery(
53-
"select p " +
54-
"from Publisher p " +
55-
"where " +
56-
" p.estYear = :estYear and " +
57-
" p.salesMonth = :salesMonth", Publisher.class)
58-
.setParameter("estYear", Year.of(2013))
59-
.setParameter("salesMonth", Month.NOVEMBER)
60-
.getSingleResult();
50+
.createQuery(
51+
"select p " +
52+
"from Publisher p " +
53+
"where " +
54+
" p.estYear = :estYear and " +
55+
" p.salesMonth = :salesMonth", Publisher.class)
56+
.setParameter("estYear", Year.of(2013))
57+
.setParameter("salesMonth", Month.NOVEMBER)
58+
.getSingleResult();
6159

6260
assertEquals("vladmihalcea.com", book.getName());
6361
});
6462
}
6563

66-
6764
@Entity(name = "Publisher")
6865
@Table(name = "publisher")
6966
@TypeDef(typeClass = YearType.class, defaultForType = Year.class)

test.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@echo off
22

3-
mvn -Ptest clean test
3+
mvn -Ptest clean test %*

0 commit comments

Comments
 (0)