2011-11-15 29 views
0


爲什麼下面的代碼部分會產生「java.lang.ClassCastException:org.dom4j.tree.DefaultElement無法轉換爲cc.co.sqeezer.model。註冊」HibernateTemplate.find返回DefaultElement而不是Hibernate實體列表

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從哪裏來?

+0

您可以發佈您正在使用的**查詢**嗎? – ManuPK

+0

查詢是'String query =「註冊爲registration.emailAddress =?」;'。參數不爲空。解決方案是添加'hibernate.default_entity_mode' – Sqeezer

回答

2

嗯,我自己找到了解決方案。 以下HibernateProperty加入

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

似乎dom4j的是在我的情況下,默認的實體模式。 看看屬性here

相關問題