简体   繁体   中英

Hibernate HQL update with where in

I would like to update status by using a list collection in "where in" clause. The parameter "UserId" is a list of an object UserId which contains two attributs like creationDate and ref.

Example:

@Modifying
@Query("update User u set u.status = ?2 where u.userid ?1")
void updateAllUserStatus( List<UserId> usersId, UserStatus status);



public class UserId implements Serializable {

    @Type(type="date")
    @Column(name = "creation_date")
    private Date creationDate;
    
    @Type(type = "string")
    @Column(name = "user_ref")
    private String ref;
}

Is update query by using a collection with object possible? Or i should update one by one?

I tried with parameter List usersId (has two attributs: creation_date and user_ref) which present as below:

Printed out usersId:

[creation_date: Mon Feb 24 00:00:00 CET 2020 | user_ref: testabc-1234-1234-1234-abcdefghigk1, creation_date: Mon Feb 24 00:00:00 CET 2020 | user_ref: testabc-1234-1234-1234-abcdefghigk2]

I got this error when i update with condition where userid in

Parameter value element [creation_date: Mon Feb 24 00:00:00 CET 2020 | user_ref: testabc-1234-1234-1234-abcdefghigk1] did not match expected type [UserId (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value element [creation_date: Mon Feb 24 00:00:00 CET 2020 | user_ref: testabc-1234-1234-1234-abcdefghigk1] did not match expected type [UserId (n/a)]

I did debuging to verify if the the list correctly filled in and it was ok.

That's totally possible, given that your database supports the row value constructor syntax and will create a SQL query similar to this:

update User u set u.status = ? where (u.ref, u.creationDate) in ((?,?),(?,?))

As Christian Beikov noticed, it is possible. I would add the following quotation from the documentation :

IN predicates performs a check that a particular value is in a list of values. Its syntax is:

 in_expression::= single_valued_expression [NOT] IN single_valued_list single_valued_list::= constructor_expression | (subquery) | collection_valued_input_parameter constructor_expression::= (expression[, expression]*)

The types of the single_valued_expression and the individual values in the single_valued_list must be consistent.

JPQL limits the valid types here to string, numeric, date, time, timestamp, and enum types , and, in JPQL, single_valued_expression can only refer to:

  • "state fields", which is its term for simple attributes. Specifically, this excludes association and component/embedded attributes.

  • entity type expressions.

In HQL, single_valued_expression can refer to a far more broad set of expression types. Single-valued association are allowed, and so are component/embedded attributes, although that feature depends on the level of support for tuple or row value constructor syntax in the underlying database. Additionally, HQL does not limit the value type in any way, though application developers should be aware that different types may incur limited support based on the underlying database vendor. This is largely the reason for the JPQL limitations.

So, the usage of this feature will tie you to the particular jpa implementation and database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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