2010-08-12 52 views
2

我遇到了Hibernate的問題,看到我的域對象爲Hibernate執行純註釋配置。Spring的Hibernate註解配置無法找到域對象

我越來越

org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User u where u.userName=:userName]

我以爲所有的都必須做的是添加packagesToScan財產爲SessionFactory並添加@Entity到域對象。我還有什麼遺漏?

<!-- Hibernate SessionFactory --> 
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="com.trx.sample.domain" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> 
     </props> 
    </property> 
    <property name="eventListeners"> 
     <map> 
      <entry key="merge"> 
       <bean 
        class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" /> 
      </entry> 
     </map> 
    </property> 
</bean> 

<context:annotation-config /> 
<tx:annotation-driven /> 

-

package com.trx.sample.domain; 

@Entity 
@Table(name = "user") 
public class User extends BaseEntity implements UserDetails { 

    private static final long serialVersionUID = 1L; 

    @Column(name = "user_name") 
    private String userName; 
    private String password; 
    private boolean enabled; 
    private String roles; 

    ... 
} 

-

@MappedSuperclass 
public class BaseEntity implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue 
    private Long id; 

    public void setId(Long id) { 
    this.id = id; 
    } 

    public Long getId() { 
    return id; 
    } 

    public boolean isNew() { 
    return (this.id == null); 
    } 
} 

-

[INFO] building session factory 
[DEBUG] Session factory constructed with filter configurations : {} 
[DEBUG] instantiating session factory with properties: {...} 
[DEBUG] initializing class SessionFactoryObjectFactory 
[DEBUG] registered: 402881e52a6b3159012a6b3163e40000 (unnamed) 
[INFO] Not binding factory to JNDI, no JNDI name configured 
[DEBUG] instantiated session factory 
[DEBUG] Checking 0 named HQL queries 
[DEBUG] Checking 0 named SQL queries 

編輯:

不知道它有所作爲,但我通過eclipse在tomcat實例上運行它。

+1

Hibernate是非常詳細的,當它啓動時,並且當它被映射應提到每個類。它說什麼? – skaffman 2010-08-12 16:48:23

+0

它並不表示它映射任何東西。 – Josh 2010-08-12 22:10:38

回答

1

掛我的頭,因爲我回答這個。 @Entity導入不正確。

此特定的域對象時,應該已經使用

import javax.persistence.Entity; 

GAH它使用

import org.hibernate.annotations.Entity; 

0

您必須在SessionFactoryBean中設置annotatedClass或packagesToScan屬性。看到Spring documentation

+0

我正在那樣做。看帖子。 – Josh 2010-08-12 14:01:28

+0

我很抱歉,我沒有看到它。我們在應用程序中使用annotatedClass,它功能正常。我試圖尋找packageToScan沒有找到類的原因,但沒有成功。嘗試設置用於登錄Hibernate的調試級別或調試Spring AnnotationSessionFactoryBean。有可能,Hibernate使用其他類路徑。 – MarrLiss 2010-08-13 07:38:31

1

只是一個想法:USER是一個reserved keyword與一些數據庫,也許這阻止了Hibernate被正確初始化。我建議逃避它:

package com.trx.sample.domain; 

@Entity 
@Table(name = "`user`") 
public class User extends BaseEntity implements UserDetails { 

    private static final long serialVersionUID = 1L; 

    @Column(name = "user_name") 
    private String userName; 
    private String password; 
    private boolean enabled; 
    private String roles; 

    ... 
} 
+0

感謝您的想法。雖然似乎沒有解決它。我只知道這是我忽略的一些愚蠢的小東西,一旦找到它,我會覺得自己像個白癡。 – Josh 2010-08-12 22:13:45

+0

@Josh然後它是別的東西:)對不起,說明明顯,但是你沒有在'SessionFactory'創建時間得到任何特定的消息嗎? '用戶'是唯一未被識別的實體嗎?你是否有來自相同包裝的其他實體能夠被正確識別? – 2010-08-12 22:20:59

0

你可能會錯過這個嗎?

<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>