希望有人能帮我解决这个问题。当我运行使用 hibernate 标准的单元测试时,我收到一些警告。具体警告是:
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
我不明白 Hibernate 在提示什么。它所讨论的列“date_created”是日期时间类型。我尝试过传递日期对象的每个其他版本、字符串、java.util.Date、java.sql.Timestamp,但这些只会导致实际错误。具体来说,它们会导致:
java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.Long
或者,当然,我尝试传递的任何其他类型而不是 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);
}
编辑抱歉延迟了,这是请求的添加内容。
@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;*/
为了简洁起见,我省略了 getter 和 setter
对于数据库来说,它有一个规则的结构。 TContent 表有:
column type
id bigint
user_id int
name varchar
date_created datetime
collection_wrapper longblob
tspoll_expire decimal
如果我可以添加任何其他内容或遗漏任何内容,请告诉我。感谢您的浏览。
最佳答案
过去我使用过以下命令,它成功地在 MySQL 日期时间字段和 java.util.Date
之间进行转换:
@Temporal(TemporalType.TIMESTAMP)
private Date dateTime;
最近与Joda-time ,以下成功在 MySQL 5.7 datetime(3) 和 org.joda.time.DateTime 之间进行转换:
@Column(columnDefinition = "DATETIME(3)")
private DateTime dateTime;
还有其他选项,但这是我目前熟悉的两个。
关于java - Hibernate 错误的日期时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35923132/