Hibernate子查询执行时出现String转Long类型转换异常
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long in Hibernate Subquery Hey there, let's break down why you're hitting this ClassCastException and how to fix it. This error almost always boils down to a type mismatch between your entity fields, database columns, or how Hibernate handles the projection in your subquery.
Common Causes & Solutions
1. Mismatched Field Types Between Entities
First, double-check the type of product_id in both UserCart and SearchHistory entities. If one is declared as String and the other as Long, Hibernate will throw this exception when trying to compare or map the values.
- Check your entity classes:
// Example of a mismatch that causes the error: // In UserCart.java private String productId; // In SearchHistory.java private Long productId; - Fix: Align the types to match your database column. If your
product_idcolumn is a numeric type (likeBIGINT), useLongin both entities. If it's a string type (likeVARCHAR), useStringinstead.
2. Incorrect Hibernate Mapping Configuration
Even if your entity fields look correct, a misconfigured Hibernate mapping can cause Hibernate to interpret the column as the wrong type.
- For Annotation-based Mapping:
Verify the@Columnannotation isn't forcing an incorrect type:// Correct for Long type @Column(name = "product_id") private Long productId; // Correct for String type @Column(name = "product_id") private String productId; - For XML-based Mapping (hbm.xml):
Check thetypeattribute in your<property>tag:<!-- Correct for Long --> <property name="productId" column="product_id" type="java.lang.Long"/> <!-- Correct for String --> <property name="productId" column="product_id" type="java.lang.String"/>
3. Type Conversion in Subquery Projection (If You Need Mixed Types)
If you can't align the types (e.g., your database stores product_id as a string but you need to compare it to a Long in SearchHistory), you can explicitly cast the value in your subquery's projection using Projections.sqlProjection:
DetachedCriteria cartProductIds = DetachedCriteria.forClass(UserCart.class,"usercart") .add(Restrictions.eq("is_deleted", false)) .add(Property.forName("usercart.user_id").eq(Property.forName("search.userId"))) .add(Property.forName("usercart.product_id").eq(Property.forName("search.productId"))) // Cast the string product_id to a BIGINT (adjust the SQL cast to match your database) .setProjection(Projections.sqlProjection( "CAST(product_id AS BIGINT) AS product_id", new String[]{"product_id"}, new org.hibernate.type.Type[]{org.hibernate.type.StandardBasicTypes.LONG} )); // Then in your main query, ensure you're matching to a Long field List<SearchHistory> history = session.createCriteria(SearchHistory.class,"search") .add(Subqueries.propertyIn("productId", cartProductIds)) // productId is Long here .list();
4. Verify Database Column Type
Don't forget to cross-check your actual database column. If product_id in the user_cart table is a VARCHAR but you're trying to map it to a Long in your entity, that's a guaranteed mismatch. Adjust either the database column type or your entity field to match.
内容的提问来源于stack exchange,提问作者Ayush Srivastava

