views:

109

answers:

3

I am having a problem with Hibernate seeing my domain objects doing a purely annotation configuration for Hibernate.

I'm getting

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

I thought all that had to be done was add the packagesToScan property for the sessionFactory and add @Entity to the domain object. What else am I missing?

<!-- 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

Edit:

Don't know if it makes a difference or not but I'm running it through eclipse on a tomcat instance.

A: 

You have to set annotatedClass or packagesToScan properties in your SessionFactoryBean. see Spring documentation

MarrLiss
I am doing that. See the post.
Josh
I am sorry, i didn't see it. We are using annotatedClass in our application and it is functional. I tried looking for reason why packageToScan didn't found classes, but without success. Try to set debug level for logging in Hibernate or debug Springs AnnotationSessionFactoryBean. It is possible, that Hibernate uses other classpath.
MarrLiss
A: 

Just an idea: USER is a reserved keyword with some databases, maybe this is preventing Hibernate from being properly initialized. I suggest escaping it:

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;

  ...
}
Pascal Thivent
Thanks for the thought. Didn't seem to fix it though. I just know this is some stupid little thing I've overlooked and am going to feel like a moron once I find it.
Josh
@Josh Then it's something else :) Sorry for stating the obvious but don't you get any particular message at `SessionFactory` creation time? Is `User` the only entity that failed to be recognized? Do you have other entities from the same package that are properly recognized?
Pascal Thivent
+1  A: 

Hanging my head in shame as I answer this. The @Entity import was incorrect.

This particular domain object used

import org.hibernate.annotations.Entity;

when it should have been using

import javax.persistence.Entity;

gah!

Josh