简体   繁体   English

类广播异常休眠映射nvarchar

[英]Classcast exception hibernate mapping nvarchar

I am trying to use DetachedCriteria to retreive Database details from the SQl server DB like below. 我正在尝试使用DetachedCriteria从SQl服务器数据库检索数据库详细信息,如下所示。 the nvarcharID is of String Type nvarcharID是字符串类型

List<POJO> pojoObj = (List<POJO>) getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(
            POJO.class).add(Restrictions.idEq(nvarcharID)));

In My POJO I have following attributes 在我的POJO中,我具有以下属性

@Id
@GeneratedValue
@Column(name = "ID")
private double ID;

@Nationalized
@Column(name = "Column1")
private String string1;

@Nationalized
@Column(name = "column2")
private String string2;

Both column1 and column 2 are of type nvarchar inSql serverDB. column1和column 2均为SQL ServerDB中的nvarchar类型。 and nvarcharID used above in the Criteria is another field in DB which is of type nvarchar in DB and I have mapped it as String. 和上面在Criteria中使用的nvarcharID是DB中的另一个字段,在DB中的类型为nvarchar,我将其映射为String。 While populating the POJO, I do not want navarcharID field to be part of my POJO. 在填充POJO时,我不希望navarcharID字段成为POJO的一部分。

For hibernate I have following dependencies in my gradle file 对于休眠,我的gradle文件中有以下依赖项

compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '4.3.11.Final'
compile group: 'javax.validation', name: 'validation-api', version: '1.0.0.GA'
compile group: 'org.hibernate', name: 'hibernate-validator-annotation-processor', version: '4.3.2.Final'
compile group: 'org.hibernate', name: 'hibernate-validator', version: '4.3.2.Final'
compile 'org.hibernate:hibernate-core:4.3.11.Final'

and my Spring dependencies are 我的Spring依赖项是

 compile group: 'org.springframework', name: 'spring-orm', version:'4.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.6.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-web', version: '4.0.2.RELEASE'

I am also setting following properties in my Hibernate config file for Dialect and ddl operations. 我还在Hibernate配置文件中为Dialect和ddl操作设置以下属性。

   @Bean
public LocalSessionFactoryBean sessionFactoryBean() {
    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    localSessionFactoryBean.setDataSource(dataSource);
    localSessionFactoryBean.setPackagesToScan(new String[] { "com.wk.cdi" });

    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");
    properties.setProperty("hibernate.use_nationalized_character_data", "true");
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    properties.setProperty("hibernate.format_sql", "true");
    properties.setProperty("hibernate.show_sql", "true");
    localSessionFactoryBean.setHibernateProperties(properties);
    return localSessionFactoryBean;
}

When I try to use hibernateTemplate to retreive the data I am getting java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double and it throws the error while using DetachedCriteria ealborated above 当我尝试使用hibernateTemplate检索数据时,我得到java.lang.ClassCastException:无法将java.lang.String强制转换为java.lang.Double,并且在上面使用DetachedCriteria时抛出了错误

PLease ignore this as there was something else. 请忽略此内容,因为还有其他问题。 It happens to be that the Restrcition.idEq needed to be replaced by Restrictions.eq 恰好是Restrcition.idEq需要由Restrictions.eq替换

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

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