Use Hibernatetemplate or Hibernatedaosupport to type conversion errors in spring

Source: Internet
Author: User

When using spring's hibernatedaosupport, the error is as follows:

Java.lang.ClassCastException:java.lang.String cannot is cast to Java.lang.Integerat Org.hibernate.type.IntegerType.set (integertype.java:64) at Org.hibernate.type.NullableType.nullSafeSet ( nullabletype.java:154) at Org.hibernate.type.NullableType.nullSafeSet (nullabletype.java:136)

The entity classes are as follows:

public class user{      private int id;      private String name;        public int getId () {          return ID;      }      public void setId (int id) {          this.id = ID;      }      Public String GetName () {          return name;      }      public void SetName (String name) {          this.name = name;      }  }


The code for the DAO layer is as follows:

public void Select (String id) {      List result = This.gethibernatetemplate (). Find ("From User where id=?",                  New Object[] {ID});  }

The reason for the error is that the id attribute of the user class in the entity class is of type Integer, but the query condition ID passed is of type string. The string is changed to an int type and is resolved incorrectly.

public void Select (int id) {      List result = This.gethibernatetemplate (). Find ("From User where id=?",              new object[] { ID});    


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use Hibernatetemplate or Hibernatedaosupport to type conversion errors in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.