`packagesToScan`不适用于@Entity和@NamedQuery

时间:2014-12-17 06:03:32

标签: spring hibernate

这是我的User.java类

package com.sudosmith.cenmesser.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * 
 * @author Vishal Joshi
 *
 */

@Entity
@Table(name = "User")
@NamedQuery(name = "findUserByName", query = "SELECT user FROM User user WHERE user.userName LIKE :queryString")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 7257051297399186919L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "username")
    private String userName;

    @Column(name = "password")
    private String password;

    @ManyToMany
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "userid"), inverseJoinColumns = @JoinColumn(name = "roleid"))
    private List<Role> roles;

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private UserStatus status;

    public User() {

    }

    public User(int id, String userName, String password, List<Role> roles,
            UserStatus status) {
        super();
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.roles = roles;
        this.status = status;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName
     *            the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the roles
     */
    public List<Role> getRoles() {
        if (roles != null) {
            return roles;
        } else {
            return new ArrayList<Role>();
        }
    }

    /**
     * @param roles
     *            the roles to set
     */
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    /**
     * @return the status
     */
    public UserStatus getStatus() {
        return status;
    }

    /**
     * @param status
     *            the status to set
     */
    public void setStatus(UserStatus status) {
        this.status = status;
    }

}

在我的spring-servlet.xml我使用packagesToScan属性来映射所有具有@Entity注释的模型类。

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryB‌ean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="hibernateProperties"> 
        <props> 
            <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
            <prop key="hibernate.show_sql">true</prop> 
        </props> 
    </property> <property name="packagesToScan"> 
        <list> 
            <value>com.sudosmith.cenmesser.model.**</value> 
        </list>
    </property>
</bean>

但添加此属性后,它无法正常工作。它不是@Entity@NamedQuery的映射。我收到的错误如mappingException: unknown @NamedQuery

编辑:Hibernate依赖

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanag‌er</artifactId> 
    <version>3.6.7.Final</version> 
</dependency> 

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>4.3.0.Final</version> 
</dependency> 

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-annotations</artifactId> 
    <version>3.3.1.GA</version> 
</dependency> 

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-core</artifactId> 
    <version>4.2.11</version> 
</dependency>

1 个答案:

答案 0 :(得分:0)

使用<value>com.sudosmith.cenmesser.model</value>

尝试