【问题标题】:Testing Hibernate mapping : no persistent classes found for query classes [duplicate]测试休眠映射:没有找到查询类的持久类[重复]
【发布时间】:2017-10-11 13:39:50
【问题描述】:

我正在使用 Hibernate JPA(注释)和 MySQL 创建一个简单的应用程序。我不断收到: WARN: HHH000183: no persistent classes found for query class: from tn.ensi.cloudtrustproject.dao.entity.Country

在测试 Hibernate 映射时。

到目前为止,我无法找到问题所在。请任何帮助。

这是我的 hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property 
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name= 
"hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name= 
"hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/TrustCloud</property>
<property name="hibernate.connection.pool_size">1</property>
<property name= 
"hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider 
</property>
<property name= "hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping class="tn.ensi.cloudtrustproject.dao.entity.Country" ></mapping>
<mapping class="tn.ensi.cloudtrustproject.dao.entity.CloudUser"></mapping>
</session-factory>
</hibernate-configuration>

这是 HibernateUtil:

public class HibernateUtil {
private static SessionFactory sessionFactory= buildSessionFactory();
private static ServiceRegistry serviceRegistry;
private static Session session=null;
private static SessionFactory buildSessionFactory(){

    try{
    Configuration configuration= new Configuration();
    configuration.configure("config/hibernate.cfg.xml");
    serviceRegistry=new 
    StandardServiceRegistryBuilder(). 
    applySettings(configuration.getProperties()).build();
    return configuration.buildSessionFactory (serviceRegistry);
    }
    catch (Throwable ex){

        System.err.println("Failed to cerate SessionFactory object"+ ex);
        throw new ExceptionInInitializerError(ex);
    }
    }
    public static SessionFactory getSessionFactory(){
    return sessionFactory;
    }
    public static Session openSession(){
    return sessionFactory.openSession();
    }
    public static Session getCurrentSession(){
    return sessionFactory.getCurrentSession();
     }

    public static void close(){
     sessionFactory.close();
     }
     }

Test.java 类:

package tn.ensi.cloudtrustproject.util;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


public class Test {

static Session session= HibernateUtil.openSession();
public static void main(String[] args) {

    session.createQuery("from tn.ensi.cloudtrustproject.dao.entity.Country 
").list();

}

}

实体类:

package tn.ensi.cloudtrustproject.dao.entity;
import java.io.Serializable;

import javax.persistence.*;

@Entity
@Table(name="country")
public class Country implements Serializable{

private static final long serialVersionUID = 1L;

@Id @GeneratedValue 
@Column(name="countryid")
private Integer countryId ;

@Column(name="countrydes")
private String countryDes;



public Country(Integer countryId, String countryDes) {
    super();
    this.countryId = countryId;
    this.countryDes = countryDes;
}
public Country() {
    super();
    // TODO Auto-generated constructor stub
}
public Integer getCountryId() {
    return countryId;
}
public void setCountryId(Integer countryId) {
    this.countryId = countryId;
}
public String getCountryDes() {
    return countryDes;
}
public void setCountryDes(String countryDes) {
    this.countryDes = countryDes;
}
public static long getSerialversionuid() {
    return serialVersionUID;
}
@Override
public String toString() {
    return "Country [countryId=" + countryId + ", countryDes=" + countryDes
            + "]";
}
}

项目: project

表已创建:

Table country

【问题讨论】:

  • 实体类被截断。您在哪里为实体定义表映射?
  • 试试 session.createQuery("from Country").list();而不是设置完整路径
  • @StanislavL 我已经在 hibernate.cfg.xml 中定义了映射,你可以在下面找到完整的实体类
  • @kimy82 我以前改过很多次,但我通常会收到错误:没有映射国家/地区我使用了anso代码from tn.ensi.cloudtrustproject.dao.entity.class.getName(),但它不起作用
  • @ManelPhD 你可以在这里找到解决方案stackoverflow.com/a/32711654/3405171

标签: java mysql spring hibernate


【解决方案1】:

您提到配置文件存在于config/hibernate.cfg.xml。请检查它是否存在于同一位置。

【讨论】:

猜你喜欢
  • 2014-04-02
  • 2023-03-22
  • 2017-06-24
  • 2013-11-07
  • 2020-09-24
  • 2013-02-09
  • 2019-06-29
  • 2016-06-21
  • 1970-01-01
相关资源
最近更新 更多