gpt4 book ai didi

java - 测试 Hibernate 映射 : no persistent classes found for query classes

转载 作者:行者123 更新时间:2023-11-29 19:00:14 25 4
gpt4 key购买 nike

我正在使用 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

最佳答案

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

关于java - 测试 Hibernate 映射 : no persistent classes found for query classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com