Skip to content

Instantly share code, notes, and snippets.

View wmuron's full-sized avatar

Wiktor Muroń wmuron

View GitHub Profile
public class ReadDateZeroWithJDBC {
public static void main(String args[]) throws ClassNotFoundException, SQLException {
Connection conn = setupJDBCAndConnect();
//expected epoch unix time
java.util.Date dateExpected = new java.util.Date(0);
//actual created_at_utc from the user record in DB
PreparedStatement stmtRead = createSelectStatement(conn, 1);
ResultSet rs = stmtRead.executeQuery();
@wmuron
wmuron / User.java
Created October 20, 2016 13:31
Entity for DbAssist article
public class User {
private int id;
private String name;
private java.util.Date createdAtUtc;
public User(int id, String name, java.util.Date createdAtUtc) {
this.id = id;
this.name = name;
this.createdAtUtc = createdAtUtc;
}
@wmuron
wmuron / User.hbm.xml
Created October 20, 2016 13:32
HBM mapping file for DbAssist article
<hibernate-mapping>
<class name="com.montrosesoftware.dbassist.User" table="users">
<id name="id" type="integer">
<column name="id" />
</id>
<property name="name" type="string" length="100" column="name"/>
<property name="createdAtUtc" type="timestamp" column="created_at_utc"/>
</class>
</hibernate-mapping>
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.getCurrentSession();
Transaction transaction = session.getTransaction();
transaction.begin();
//insert one user
String sql = "INSERT INTO users (id, name, created_at_utc)"
+ "VALUES (1, 'Adam Spring', '2016-06-12 14:54:15')";
Query query = session.createSQLQuery(sql);
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
//prepare date
String createdAtUtcStr = "2016-04-24 9:54:23";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format.parse(createdAtUtcStr);
import org.hibernate.type.DateType;
public class UtcDateType extends DateType {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
@Override
public Object get(ResultSet rs, String name) throws SQLException {
return rs.getDate(name, Calendar.getInstance(UTC));
}
@wmuron
wmuron / User.hbm.xml
Created October 20, 2016 13:36
Line showing how to use fix
...
<property name="createdAt" type="com.montrosesoftware.dbassist.types.UtcDateType" column="created_at"/>
...
@TypeDef(name = "UtcDateType", defaultForType = Date.class, typeClass = UtcDateType.class),
package com.montrosesoftware.dbassist.types;
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
Root<User> userRoot = criteriaQuery.from(User.class);
//Specification<User> specs = (root, query, cb) ->
// cb.equal(root.get("createdAt"), utcDate);
String paramName = "pn";
Specification<User> specs = (root, query, cb) ->
cb.equal(root.get("createdAt"), cb.parameter(Date.class, paramName));
ConditionsBuilder conditionsBuilder = new ConditionsBuilder();
Condition cond = conditionsBuilder.equal("createdAt", utcDate);
conditionsBuilder.apply(cond);
List<User> results = uRepo.find(conditionsBuilder);