簡體   English   中英

休眠日期時間值不正確

[英]Hibernate Incorrect datetime value

希望有人可以幫我解決這個問題。 當我運行使用休眠條件的單元測試時,我收到一些警告。 具體警告是:

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;*/

為了簡潔起見,我省略了getter和setter方法

至於數據庫,它有一個規則的結構。 TContent表具有:

column               type
id                  bigint
user_id             int
name                varchar
date_created        datetime
collection_wrapper  longblob
tspoll_expire       decimal

請讓我知道我是否可以添加其他內容或錯過任何內容。 謝謝參觀。

過去,我使用了以下代碼,該代碼成功地在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;

會有其他選擇,但是這是我目前熟悉的兩個。

當參數hibernate.hbm2ddl.auto設置為update時,Hibernate有一個錯誤(某種)。如果表包含的文件具有相同的名稱(例如,來自先前的部署),它將僅保留字段“ as is”,並且不更改字段類型。 我認為,以前您將date_created設置為Date類型,但后來將其切換為long類型。

可能的解決方案:

  1. 更改java bean字段類型long dateCreated ; Date dateCreated; 並在代碼中使用日期類型。
  2. 根據您的java類手動更改數據庫結構。 alter table content alter column date_created TYPE bigint我不確定mysql是否允許這樣做,在這種情況下,您應該編寫遷移過程。
  3. 刪除數據庫並使用休眠方式重新創建它(僅當內容不重要/測試/垃圾時)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM