-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In some legacy schemas it might be required to map a YearMonth to a Timestamp value, rather than the Date value that we currently support. This may for example be chosen for easy range comparisions to other timestamp values without any casting being required. This change adds such a YearMonthTimestampType. While I think YearMonthDateType will generally be the preferred type, this at least should smoothen the transition from a YearMonth modelled as a timestamp to a YearMonth value.
- Loading branch information
1 parent
5b9a730
commit 956747d
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...-types-52/src/main/java/com/vladmihalcea/hibernate/type/basic/YearMonthTimestampType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.vladmihalcea.hibernate.type.basic; | ||
|
||
import com.vladmihalcea.hibernate.type.AbstractHibernateType; | ||
import com.vladmihalcea.hibernate.type.basic.internal.YearMonthTypeDescriptor; | ||
import com.vladmihalcea.hibernate.type.util.Configuration; | ||
import org.hibernate.type.descriptor.sql.DateTypeDescriptor; | ||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor; | ||
|
||
import java.time.YearMonth; | ||
|
||
/** | ||
* Maps a Java {@link YearMonth} object to a {@code TIMESTAMP} column type. | ||
* <p> | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
public class YearMonthTimestampType | ||
extends AbstractHibernateType<YearMonth> { | ||
|
||
public static final YearMonthTimestampType INSTANCE = new YearMonthTimestampType(); | ||
|
||
public YearMonthTimestampType() { | ||
super( | ||
TimestampTypeDescriptor.INSTANCE, | ||
YearMonthTypeDescriptor.INSTANCE | ||
); | ||
} | ||
|
||
public YearMonthTimestampType(Configuration configuration) { | ||
super( | ||
TimestampTypeDescriptor.INSTANCE, | ||
YearMonthTypeDescriptor.INSTANCE, | ||
configuration | ||
); | ||
} | ||
|
||
public String getName() { | ||
return "yearmonth-timestamp"; | ||
} | ||
|
||
@Override | ||
protected boolean registerUnderJavaType() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLYearMonthTimestampTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.vladmihalcea.hibernate.type.basic; | ||
|
||
import com.vladmihalcea.hibernate.type.util.AbstractPostgreSQLIntegrationTest; | ||
import org.hibernate.Session; | ||
import org.hibernate.annotations.NaturalId; | ||
import org.hibernate.annotations.TypeDef; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import java.time.YearMonth; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
public class PostgreSQLYearMonthTimestampTest extends AbstractPostgreSQLIntegrationTest { | ||
|
||
@Override | ||
protected Class<?>[] entities() { | ||
return new Class<?>[]{ | ||
Book.class | ||
}; | ||
} | ||
|
||
@Test | ||
public void test() { | ||
doInJPA(entityManager -> { | ||
Book book = new Book(); | ||
book.setIsbn("978-9730228236"); | ||
book.setTitle("High-Performance Java Persistence"); | ||
book.setPublishedOn(YearMonth.of(2016, 10)); | ||
|
||
entityManager.persist(book); | ||
}); | ||
|
||
doInJPA(entityManager -> { | ||
Book book = entityManager | ||
.unwrap(Session.class) | ||
.bySimpleNaturalId(Book.class) | ||
.load("978-9730228236"); | ||
|
||
assertEquals(YearMonth.of(2016, 10), book.getPublishedOn()); | ||
}); | ||
|
||
doInJPA(entityManager -> { | ||
Book book = entityManager | ||
.createQuery( | ||
"select b " + | ||
"from Book b " + | ||
"where " + | ||
" b.title = :title and " + | ||
" b.publishedOn = :publishedOn", Book.class) | ||
.setParameter("title", "High-Performance Java Persistence") | ||
.setParameter("publishedOn", YearMonth.of(2016, 10)) | ||
.getSingleResult(); | ||
|
||
assertEquals("978-9730228236", book.getIsbn()); | ||
}); | ||
} | ||
|
||
|
||
@Entity(name = "Book") | ||
@Table(name = "book") | ||
@TypeDef(typeClass = YearMonthTimestampType.class, defaultForType = YearMonth.class) | ||
public static class Book { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@NaturalId | ||
private String isbn; | ||
|
||
private String title; | ||
|
||
@Column(name = "published_on", columnDefinition = "date") | ||
private YearMonth publishedOn; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(String isbn) { | ||
this.isbn = isbn; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public YearMonth getPublishedOn() { | ||
return publishedOn; | ||
} | ||
|
||
public void setPublishedOn(YearMonth publishedOn) { | ||
this.publishedOn = publishedOn; | ||
} | ||
} | ||
} |