希望有人可以为我解决这个问题。当我运行使用休眠标准的单元测试时,我收到一些警告。具体警告是:
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'是datetime类型。我试过传递日期对象的每个其他版本,一个字符串,一个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;*/
我为了简洁省略了getters和setter
对于数据库,它有一个规则的结构。 TContent表具有:
column type
id bigint
user_id int
name varchar
date_created datetime
collection_wrapper longblob
tspoll_expire decimal
如果我可以添加任何其他内容或遗漏任何内容,请告知我们。谢谢你看看。
答案 0 :(得分:5)
过去我使用过以下内容,可以在MySQL datetime字段和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;
还有其他选择,但这些是我目前熟悉的两种选择。
答案 1 :(得分:1)
当参数hibernate.hbm2ddl.auto
设置为update
时,Hibernate有一个错误(某种类型)如果表包含具有相同名称的字段,例如从之前的部署中,它只是保留字段&#39; #39;并且不会改变字段类型。
我想,之前您将date_created
设置为日期类型,但稍后将其切换为long。
可能的解决方案:
long dateCreated
;到Date dateCreated;
并在代码中使用日期类型。 alter table content alter column date_created TYPE bigint
我不确定mysql是否允许这样做,在这种情况下你应该编写迁移过程。