码迷,mamicode.com
首页 > 其他好文 > 详细

no persistent classes found for query class: FROM com.vrv.paw.domain.User

时间:2016-01-25 13:06:33      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

在整合Spring+Hibernate时报该错误,sessionFactory配置如下:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    .......
        <!-- 自动扫描注解方式配置的hibernate类文件 -->
        <property name="packagesToScan">  //最后发现是该处的错误
            <list>
                <value>sys.model</value>
            </list>
        </property>
    .......
</bean>

正如上面XML配置的那样,packageToScan是配置以注解方式配置Hibernate类,即如下这样的方式:

package sys.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "User")
public class UserInfo implements Serializable {
    private static final long serialVersionUID = 8427996478439081619L;
    // Fields
    private String id;
    private String name;
    private String pwd;
    private Date createdatetime;
    private Date modifydatetime;
    /** full constructor */
    public UserInfo(String id, String name, String pwd, Date createdatetime, Date modifydatetime) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.createdatetime = createdatetime;
        this.modifydatetime = modifydatetime;
    }

    @Id
    @Column(name = "ID", unique = true, nullable = false, length = 36)
    public String getId() {
        return this.id;
    }

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

    @Column(name = "NAME", unique = true, nullable = false, length = 100)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "PWD", nullable = false, length = 32)
    public String getPwd() {
        return this.pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Temporal(TemporalType.TIMESTAMP)
    // 如果这里是这种TemporalType.DATE,那么时间在数据库中存的时候会是"2015-07-16 00:00:00"这种形式,时分秒都为零
    @Column(name = "CREATEDATETIME", length = 7)
    public Date getCreatedatetime() {
        return this.createdatetime;
    }

    public void setCreatedatetime(Date createdatetime) {
        this.createdatetime = createdatetime;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "MODIFYDATETIME", length = 7)
    public Date getModifydatetime() {
        return this.modifydatetime;
    }

    public void setModifydatetime(Date modifydatetime) {
        this.modifydatetime = modifydatetime;
    }

    @Override
    public String toString() {
        return "[ID:" + id + " Name:" + name + " PassWord:" + pwd + " Createdatetime:" + createdatetime + " Modifydatetiem:" + modifydatetime + "]";
    }
}

但是我整合式采用的*.hbm.xml的方式

技术分享

这种方式最方便的方式是采用“mappingDirectoryLocations”

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    .......
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:com/vrv/paw/domain</value>
            </list>
        </property>
</bean>

扩展:

mappingResources、mappingLocations、mappingDirectoryLocations
他们的区别:   
1. mappingResources:指定classpath下具体映射文件名   

<property name="mappingResources">   
    <value>petclinic.hbm.xml </value>   
</property>   

2. mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等   

<property name="mappingLocations">   
    <value>/WEB-INF/petclinic.hbm.xml </value>   
</property>  
<property name="mappingLocations">   
    <value>classpath:/com/company/domain/petclinic.hbm.xml </value>   
</property>   

也可以用通配符指定,‘*‘指定一个文件(路径)名,‘**‘指定多个文件(路径)名,例如:   

<property name="mappingLocations">   
    <value>classpath:/com/company/domainmaps/*.hbm.xml </value>   
</property>   

上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件   
 
3. mappingDirectoryLocations:指定映射的文件路径   

<property name="mappingDirectoryLocations">  <list>   
  <value>WEB-INF/HibernateMappings</value>   
  </list>   
</property>   

也可以通过classpath来指出   

<property name="mappingDirectoryLocations">   
  <list>   
  <value>classpath:/XXX/package/</value>   
  </list>   
</property>  

总结:注解方式配置的hibernate类 与  *.hbm.xml  方式配置的类,在配置SessionFactory时配置方式是不同的,这是我以前没有注意的

 

no persistent classes found for query class: FROM com.vrv.paw.domain.User

标签:

原文地址:http://www.cnblogs.com/liaojie970/p/5156949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!