【问题标题】:HibernateTemplate.find returns list of DefaultElement instead of Hibernate EntityHibernateTemplate.find 返回 DefaultElement 列表而不是 Hibernate Entity
【发布时间】:2011-11-15 13:15:59
【问题描述】:


为什么以下部分代码会产生“java.lang.ClassCastException: org.dom4j.tree.DefaultElement cannot be cast to cc.co.sqeezer.model.Registration”

List list = this.getHibernateTemplate().find(query, parameters); 
if (list.size() > 0) { 
        registration = (Registration) list.get(0); // here is ClassCastException
}

映射是:

<class name="Registration" table="registration" dynamic-insert="true" dynamic-update="true" optimistic-lock="version">
<meta attribute="implement-equals">true</meta>
<meta attribute="implement-tostring">true</meta>

<id name="registrationId" type="integer" column="registration_id" unsaved-value="none">
  <meta attribute="scope-set">public</meta>
  <meta attribute="use-in-tostring">true</meta>

  <!-- generator class="native"></generator -->
  <generator class="sequence">
    <param name="sequence">registration_registration_id_seq</param>
  </generator>
</id>

<property name="emailAddress" column="email" type="string" not-null="true">
  <meta attribute="use-in-equals">true</meta>
  <meta attribute="use-in-tostring">true</meta>
</property>
<property name="password" column="password" type="string" length="50">
  <meta attribute="use-in-equals">true</meta>
  <meta attribute="use-in-tostring">true</meta>
</property>

applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource"><ref local="dataSource" /></property>
    <property name="mappingResources">
        <list>
            <value>cc/co/sqeezer/model/Mappings.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

DefaultElement 来自哪里?

【问题讨论】:

  • 您可以发布您正在使用的查询吗?
  • 查询是String query = "from Registration as registration where registration.emailAddress = ?";。参数不为空。解决方案是添加hibernate.default_entity_mode

标签: hibernate spring hibernate-mapping classcastexception


【解决方案1】:

好吧,我自己找到了解决方案。 添加了以下 HibernateProperty

<prop key="hibernate.default_entity_mode">pojo</prop>

似乎 dom4j 是我的默认实体模式。 查看属性here

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多