Hibernate日期时间值不正确

car*_*ian 2 java spring datetime hibernate spring-data

希望有人可以为我清除这一点.当我运行使用休眠标准的单元测试时,我收到一些警告.具体警告是:

Mar 10, 2016 11:48:31 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 1292, SQLState: 22007
Mar 10, 2016 11:48:31 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: Incorrect datetime value: '1454684370' for column 'date_created' at row 1
Mar 10, 2016 11:48:31 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 1292, SQLState: 22007
Mar 10, 2016 11:48:31 AM  org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: Incorrect datetime value: '1454684700' for column 'date_created' at row 1
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚Hibernate在抱怨什么.它所谈论的专栏'date_created'是datetime类型.我试过传递日期对象的每个其他版本,一个字符串,一个java.util.Date,一个java.sql.Timestamp,但那些只会导致实际错误.特别是他们导致:

java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.Long
Run Code Online (Sandbox Code Playgroud)

或者,当然,我尝试过的其他任何类型而不是Long.我传入的时间是大纪元,但由于某种原因我得到了这些错误,单位测试没有通过.

此外,如果它可能有所帮助,这里是测试中的特定代码:

public List<Content> findByCollectionName(String collectionName, Long exclusiveBegin, Long inclusiveEnd, Long expiration)
{
    if(collectionName == null)
        return null;

    Session session = currentSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(Content.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.createAlias("collections", "cols");
    criteria
        .add(Restrictions.and(Restrictions.eq("cols.name", collectionName), buildCriterion(exclusiveBegin, inclusiveEnd, expiration)));

    List<Content> list = criteria.list();

    session.getTransaction().commit();

    return list;
}

private Criterion buildCriterion(Long exclusiveBegin, Long inclusiveEnd, Long expiration)
{
    List<Criterion> criterion = new ArrayList<Criterion>();
    if(exclusiveBegin != null)
        criterion.add(Restrictions.gt("dateCreated", exclusiveBegin));
    if(inclusiveEnd != null)
        criterion.add(Restrictions.le("dateCreated", inclusiveEnd));
    if(expiration != null)
        criterion.add(Restrictions.ge("dateCreated", expiration));

    Criterion[] array = new Criterion[criterion.size()];
    for(int j = 0; j < array.length; j++)
    {
        array[j] = criterion.get(j);
    }

    return Restrictions.and(array);
}
Run Code Online (Sandbox Code Playgroud)

编辑抱歉延迟,这是所要求的补充.

@Entity
@Table(name = "content")
public class Content implements Serializable
{
private static final long serialVersionUID = -8483381938400121236L;

public Content()
{
}

public Content(String messageId, ContentBlock block) throws NullPointerException
{
    if(messageId == null || block == null)
        throw new NullPointerException("Null objects passed for Content object creation");
    this.messageId = messageId;
    this.setContentBlock(block);
    this.dateCreated = System.currentTimeMillis();
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "BIGINT UNSIGNED")
private int id;

@Column(name = "message_id")
private String messageId;

@Column(name = "date_created")
private long dateCreated;

@Column(name = "content_wrapper", columnDefinition = "longblob")
private byte[] contentWrapper;

@ManyToMany(mappedBy = "contentBlocks")
private List<TCollection> collections;

/*@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "message_id")
private TMessage message;*/
Run Code Online (Sandbox Code Playgroud)

为简洁起见,我省略了getter和setter

至于数据库,它有一个规则的结构.TContent表具有:

column               type
id                  bigint
user_id             int
name                varchar
date_created        datetime
collection_wrapper  longblob
tspoll_expire       decimal
Run Code Online (Sandbox Code Playgroud)

如果我可以添加任何其他内容或遗漏任何内容,请告知我们.谢谢参观.

Ste*_*ers 5

在过去,我使用了以下内容,它成功地在MySQL datetime字段和java.util.Date:

@Temporal(TemporalType.TIMESTAMP)
private Date dateTime;
Run Code Online (Sandbox Code Playgroud)

最近在Joda-time中,以下成功转换MySQL 5.7 datetime(3)和org.joda.time.DateTime:

@Column(columnDefinition = "DATETIME(3)")
private DateTime dateTime;
Run Code Online (Sandbox Code Playgroud)

还有其他选择,但这些是我目前熟悉的两个.