org.hibernate.TypeMismatchException: Provided id of the wrong type for class
2014-02-13 00:06
525 查看
在使用Atomikos写demo的时候,用spring整合hibernate写一个查询,报错:org.hibernate.TypeMismatchException: Provided id of the wrong type for class aniyo.jta.atomikos.domain.BankAccount. Expected: class java.lang.Integer, got class java.lang.String方法如下:Java代码
@Override
public BankAccount getByUsername(String username) {
return (BankAccount) this.getSession().get(BankAccount.class, username);
}
因为没怎么用过hibernate,很多东西也不熟悉,到网上查了一下相关资料,后面才发现,this.getSession().get()方法,是通过id来查询的,所以这里会报错说,提供id类型错误,我这里传的是一个String的username,所以这里要通过username查询,可能过createQuery来查;最后修改如下:Java代码
@Override
@SuppressWarnings("unchecked")
public BankAccount getByUserName(String username) {
List<BankAccount> list = this.getSession().createQuery(
"select t from BankAccount t where t.username=:userName")
.setParameter("userName", username).list();
return list.isEmpty() ? null : list.get(0);
}
@Override
public BankAccount getByUsername(String username) {
return (BankAccount) this.getSession().get(BankAccount.class, username);
}
因为没怎么用过hibernate,很多东西也不熟悉,到网上查了一下相关资料,后面才发现,this.getSession().get()方法,是通过id来查询的,所以这里会报错说,提供id类型错误,我这里传的是一个String的username,所以这里要通过username查询,可能过createQuery来查;最后修改如下:Java代码
@Override
@SuppressWarnings("unchecked")
public BankAccount getByUserName(String username) {
List<BankAccount> list = this.getSession().createQuery(
"select t from BankAccount t where t.username=:userName")
.setParameter("userName", username).list();
return list.isEmpty() ? null : list.get(0);
}
相关文章推荐
- Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class [Lja
- hibernate.TypeMismatchException: Provided id of the wrong type for class
- TypeMismatchException: Provided id of the wrong type for class zhongfucheng.user.entity.User.
- Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type
- TypeMismatchException: Provided id of the wrong type for class zhongfucheng.user.entity.User.
- hibernate 错误:Provided id of the wrong type for class com.lhaix.pojo.Test. Expected: class java.la
- Hibernate异常:Provided id of the wrong type for class
- hibernate 中 Provided id of the wrong type for class 异常
- org.hibernate.WrongClassException: Object with id: 3 was not of the specified subclass
- Provided id of the wrong type for class
- Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class java.lang.Long
- SSH报错之 Provided id of the wrong type for class model.User. Expected: class java.lang
- org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String
- org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before
- org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before
- org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()
- Hibernate报错:org.hibernate.id.IdentifierGenerationException:ids for this class must be manually assigned before calling save()
- org.springframework.data.mapping.model.MappingException: No id property found for object of type
- org.hibernate.PropertyNotFoundException Could not find a getter for id in class
- Hibernate报错:org.hibernate.id.IdentifierGenerationException:ids for this class must be manually assig