I just noticed that Hibernate automatically assumes data types for query parameters based on names (maybe column or annotated class). I'm trying to store a unix timestamp as long in a BIGINT column in a MySQL database. When the name is timestamp in the database and in my (JPA) annotated class, Hibernate automatically assumes, that it is a temporal value and thus I get an exception, when I try to bind a long value for this column in a query, because Hibernate expects a java.util.Date. When I rename this column in the database and in the class to 'foobar' everything works as expected. How can I stop Hibernate doing such stupid assumptions?
Edit:
DB Table:
CREATE TABLE candle
(
`open` decimal(25,15) NOT NULL,
`high` decimal(25,15) NOT NULL,
`low` decimal(25,15) NOT NULL,
`close` decimal(25,15) NOT NULL,
`volume` decimal(15,2) NOT NULL,
`symbol` varchar(255) NOT NULL,
`exchange` varchar(255) NOT NULL,
`timestamp` bigint NOT NULL,
`width` bigint NOT NULL,
PRIMARY KEY(`symbol`, `exchange`, `timestamp`, `width`),
KEY idx_symbol (`symbol`),
KEY idx_exchange (`exchange`),
KEY idx_timestamp (`timestamp`),
KEY idx_width (`width`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
JPA class:
@Entity(name="candle")
public class JpaCandle {
@EmbeddedId
private Key id;
@Column
private BigDecimal open;
@Column
private BigDecimal high;
@Column
private BigDecimal low;
@Column
private BigDecimal close;
@Column
private BigDecimal volume;
public Key getId() {
return id;
}
@Embeddable
public static class Key implements Serializable {
@Column
private String symbol;
@Column
private String exchange;
@Column
private long timestamp;
@Column
private long width;
}
}
Query:
TypedQuery<JpaCandle> q = em.createQuery("FROM candle WHERE symbol = :symbol AND width = :width AND timestamp >= :from AND timestamp <= :until ORDER BY timestamp ASC", JpaCandle.class);
q.setParameter("from", from.toEpochMilli());
q.setParameter("until", until.toEpochMilli());
q.setParameter("symbol", symbol);
q.setParameter("width", width.getSeconds());
List<JpaCandle> jpaCandles = q.getResultList();