简体   繁体   English

如何通过hibernate将空字符串传递给Date类型列

[英]How to pass an empty string to a Date type column through hibernate

I am trying to pass an empty string that is "" to a Date type column Logout_date through hibernate: 我试图通过hibernate将一个空字符串""传递给Date类型列Logout_date

query.setParameter(3, "");

But an exception is thrown : 但抛出异常:

java.lang.String cannot be cast to java.util.Date.

How can I achieve this.? 我怎样才能做到这一点。

EDIT: 编辑:

String updq2 = "update TblUser a set loggedStatus = ? ,lockedStatus= ?,currentAttempts = ?,logoutDate = ? where upper(a.id)=?";//,lockedStatus= ?, currentAttempts = ?, logoutDate = ? where upper(a.id) = ?";
            Session newSession2=factory.openSession();
            Transaction tx1=newSession2.beginTransaction();
            Query query =newSession2.createQuery(updq2);
            query.setParameter(0, (byte)9);
            query.setParameter(1,"1" );
            query.setParameter(2, 0);
            query.setParameter(3, "");
            query.setParameter(4,  userName.toUpperCase());
query.executeUpdate();
tx1.commit();
newSession2.close();
  1. If your inserting data to your database and the database allows null values for that column just don't set that parameter in your query. 如果向数据库和数据库插入数据允许该列的null值,则不要在查询中设置该参数。 In this case it's automatically set to null . 在这种情况下,它会自动设置为null

  2. If your having an update statement and want to set an existing value to null you could try something like that: 如果你有一个更新语句并想要将现有值设置为null你可以尝试这样的事情:

     query.setParameter(3, PreparedStatement.setNull()); 

Hope that helps! 希望有所帮助!

EDIT: 编辑:

    String updq2;
    bool isNull = false;
    //If your data is null and you want to set it null use this 
    if(yourDate == null)
    {
        updq2 = "update TblUser a set loggedStatus = ? ,lockedStatus= ?,currentAttempts = ?,logoutDate = NULL where upper(a.id)=?";//,lockedStatus= ?, currentAttempts = ?, logoutDate = ? where upper(a.id) = ?";
        isNull = true;
    } //If it isn't null and you want to set and value use this
    else
    {
        updq2 = "update TblUser a set loggedStatus = ? ,lockedStatus= ?,currentAttempts = ?,logoutDate = ? where upper(a.id)=?";//,lockedStatus= ?, currentAttempts = ?, logoutDate = ? where upper(a.id) = ?";

    } 

    Session newSession2=factory.openSession();
    Transaction tx1=newSession2.beginTransaction();
    Query query =newSession2.createQuery(updq2);
    query.setParameter(0, (byte)9);
    query.setParameter(1,"1" );
    query.setParameter(2, 0);

    if(isNull)
    {
         query.setParameter(3,  userName.toUpperCase());
    }
    else
    {
         query.setParameter(3, yourDataHere);
         query.setParameter(4,  userName.toUpperCase());
    }

The best practice would be You check if date is null. 最佳做法是检查date是否为null。 If null you should avoid adding parameter for null value of type date. 如果为null,则应避免为date类型的null值添加参数。 Hibernate does not automatically convert string to date. Hibernate不会自动将字符串转换为日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM