|
| 1 | +package com.vladmihalcea.hibernate.type.basic; |
| 2 | + |
| 3 | +import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest; |
| 4 | +import com.vladmihalcea.hibernate.type.util.AbstractPostgreSQLIntegrationTest; |
| 5 | +import org.hibernate.Session; |
| 6 | +import org.hibernate.annotations.TypeDef; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import javax.persistence.*; |
| 10 | +import java.time.MonthDay; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.TimeZone; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Mladen Savic ([email protected]) |
| 18 | + */ |
| 19 | +public class MySQLMonthDayDateTest extends AbstractMySQLIntegrationTest { |
| 20 | + |
| 21 | + public static final String COLUMN_TYPE = "date"; |
| 22 | + |
| 23 | + @Override |
| 24 | + protected Class<?>[] entities() { |
| 25 | + return new Class<?>[]{Season.class}; |
| 26 | + } |
| 27 | + |
| 28 | + private TimeZone defaultTimeZone; |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void afterInit() { |
| 32 | + defaultTimeZone = TimeZone.getDefault(); |
| 33 | + TimeZone.setDefault(TimeZone.getTimeZone("Europe/Athens")); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void destroy() { |
| 38 | + super.destroy(); |
| 39 | + TimeZone.setDefault(defaultTimeZone); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testCreationAndFetchById(){ |
| 44 | + Season season = createEntity("Summer", MonthDay.of(6,21), MonthDay.of(9,22)); |
| 45 | + |
| 46 | + doInJPA(entityManager -> { |
| 47 | + Season summer = entityManager.unwrap(Session.class).find(Season.class, season.getId()); |
| 48 | + |
| 49 | + assertEquals(summer.getBeginningOfSeason(), MonthDay.of(6,21)); |
| 50 | + assertEquals(summer.getEndOfSeason(), MonthDay.of(9,22)); |
| 51 | + }); |
| 52 | + |
| 53 | + assertEquals(COLUMN_TYPE, getColumnType("end_of_season") ); |
| 54 | + assertEquals(COLUMN_TYPE, getColumnType("beginning_of_season") ); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testFetchWithQuery(){ |
| 60 | + createEntity("Winter", MonthDay.of(12,21), MonthDay.of(3,20)); |
| 61 | + |
| 62 | + doInJPA(entityManager -> { |
| 63 | + Season seasonQ = entityManager |
| 64 | + .createQuery( |
| 65 | + "select s " + |
| 66 | + "from Season s " + |
| 67 | + "where " + |
| 68 | + "s.beginningOfSeason = :beginningOfSeason", Season.class) |
| 69 | + .setParameter("beginningOfSeason", MonthDay.of(12,21)) |
| 70 | + .getSingleResult(); |
| 71 | + |
| 72 | + assertEquals("Winter", seasonQ.getName()); |
| 73 | + }); |
| 74 | + |
| 75 | + assertEquals(COLUMN_TYPE, getColumnType("end_of_season") ); |
| 76 | + assertEquals(COLUMN_TYPE, getColumnType("beginning_of_season") ); |
| 77 | + } |
| 78 | + |
| 79 | + public Season createEntity(String name, MonthDay beginning, MonthDay end){ |
| 80 | + Season season = new Season(); |
| 81 | + season.setName(name); |
| 82 | + season.setBeginningOfSeason(beginning); |
| 83 | + season.setEndOfSeason(end); |
| 84 | + |
| 85 | + doInJPA(entityManager -> { |
| 86 | + entityManager.persist(season); |
| 87 | + }); |
| 88 | + |
| 89 | + return season; |
| 90 | + } |
| 91 | + |
| 92 | + public String getColumnType(String column){ |
| 93 | + ArrayList<String> results = new ArrayList<>(1); |
| 94 | + doInJPA(entityManager -> { |
| 95 | + Object result = entityManager.createNativeQuery("SELECT data_type FROM information_schema.columns WHERE \n" + |
| 96 | + "table_name = 'season' AND column_name = :column_name") |
| 97 | + .setParameter("column_name", column) |
| 98 | + .getSingleResult(); |
| 99 | + results.add((String) result); |
| 100 | + }); |
| 101 | + return results.get(0); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + @Entity(name = "Season") |
| 106 | + @Table(name = "season") |
| 107 | + @TypeDef(typeClass = MonthDayDateType.class, defaultForType = MonthDay.class) |
| 108 | + public static class Season { |
| 109 | + |
| 110 | + @Id |
| 111 | + @GeneratedValue |
| 112 | + private Long id; |
| 113 | + private String name; |
| 114 | + @Column(name = "beginning_of_season", columnDefinition = "date") |
| 115 | + private MonthDay beginningOfSeason; |
| 116 | + @Column(name = "end_of_season", columnDefinition = "date") |
| 117 | + private MonthDay endOfSeason; |
| 118 | + |
| 119 | + public Long getId() { |
| 120 | + return id; |
| 121 | + } |
| 122 | + |
| 123 | + public void setId(Long id) { |
| 124 | + this.id = id; |
| 125 | + } |
| 126 | + |
| 127 | + public String getName() { |
| 128 | + return name; |
| 129 | + } |
| 130 | + |
| 131 | + public void setName(String name) { |
| 132 | + this.name = name; |
| 133 | + } |
| 134 | + |
| 135 | + public MonthDay getBeginningOfSeason() { |
| 136 | + return beginningOfSeason; |
| 137 | + } |
| 138 | + |
| 139 | + public void setBeginningOfSeason(MonthDay beginningOfSeason) { |
| 140 | + this.beginningOfSeason = beginningOfSeason; |
| 141 | + } |
| 142 | + |
| 143 | + public MonthDay getEndOfSeason() { |
| 144 | + return endOfSeason; |
| 145 | + } |
| 146 | + |
| 147 | + public void setEndOfSeason(MonthDay endOfSeason) { |
| 148 | + this.endOfSeason = endOfSeason; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments