2010-08-12 55 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"/>