Last active
April 17, 2016 11:08
-
-
Save michael-simons/5b4af0c8ca4360b708a37c0c63455b14 to your computer and use it in GitHub Desktop.
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
package org.hibernate.bugs; | |
import java.time.LocalDate; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.Persistence; | |
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
/** | |
* This template demonstrates how to develop a test case for Hibernate ORM, | |
* using the Java Persistence API. | |
*/ | |
public class JPAUnitTestCase { | |
private EntityManagerFactory entityManagerFactory; | |
@Before | |
public void init() { | |
entityManagerFactory = Persistence.createEntityManagerFactory("templatePU"); | |
} | |
@After | |
public void destroy() { | |
entityManagerFactory.close(); | |
} | |
@Entity | |
public class Event { | |
@Id | |
@GeneratedValue | |
private Long id; | |
private ZonedDateTime createdOn; | |
public Event() { | |
} | |
public Event(ZonedDateTime createdOn) { | |
this.createdOn = createdOn; | |
} | |
public Long getId() { | |
return id; | |
} | |
public ZonedDateTime getCreatedOn() { | |
return createdOn; | |
} | |
} | |
@Test | |
public void hhh123Test() throws Exception { | |
EntityManager entityManager = entityManagerFactory.createEntityManager(); | |
entityManager.getTransaction().begin(); | |
entityManager.persist(new Event(ZonedDateTime.of(2016, 4, 1, 12, 0, 0, 0, ZoneId.systemDefault()))); | |
entityManager.persist(new Event(ZonedDateTime.of(2016, 4, 2, 12, 0, 0, 0, ZoneId.systemDefault()))); | |
entityManager.persist(new Event(ZonedDateTime.of(2016, 4, 3, 12, 0, 0, 0, ZoneId.systemDefault()))); | |
entityManager.getTransaction().commit(); | |
List<Event> allEvents = entityManager.createQuery("Select e from Event e order by e.createdOn asc").getResultList(); | |
Assert.assertEquals(3, allEvents.size()); | |
Assert.assertEquals(LocalDate.of(2016, 4, 1), allEvents.get(0).getCreatedOn().toLocalDate()); | |
// All events after a certain _day_ | |
List<Event> newEvents = entityManager | |
.createQuery("Select e from Event e where date(e.createdOn) >= date(:createdOn) order by e.createdOn asc") | |
.setParameter("createdOn", LocalDate.of(2016, 4, 2).atStartOfDay(ZoneId.systemDefault())) | |
.getResultList(); | |
// Won't work either… though i have a converter for localdates as well4 | |
/* | |
List<Event> newEvents = entityManager | |
.createQuery("Select e from Event e where date(e.createdOn) >= :createdOn order by e.createdOn asc") | |
.setParameter("createdOn", LocalDate.of(2016, 4, 2)) | |
.getResultList(); | |
*/ | |
Assert.assertEquals(2, newEvents.size()); | |
Assert.assertEquals(LocalDate.of(2016, 4, 2), newEvents.get(0).getCreatedOn().toLocalDate()); | |
entityManager.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment