Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

spring - `packagesToScan` not working for @Entity and @NamedQuery

This is my User.java class

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;
    }

}

in my spring-servlet.xml I have used the packagesToScan property to map all the model classes which has @Entity annotation.

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

But after adding this property its not working. Its not mapping @Entity and @NamedQuery. I am getting error like mappingException: unknown @NamedQuery.

EDIT: Hibernate Dependencies

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try it with <value>com.sudosmith.cenmesser.model</value>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...