gpt4 book ai didi

java - MySQL 表未被 Hibernate 5+ 映射

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

我正在尝试使用名为 world 的 MySQL 内置数据库来学习 Hibernate。它有三个表,分别称为城市、国家和国家语言。我要做的是执行 SQL 语句 SELECT * FROM world.city;。当我运行我的项目时出现错误

org.hibernate.hql.internal.ast.QuerySyntaxException: City is not mapped [from City]

我正在使用 IntelliJ IDEA 和 Hibernate 5.2.8。

我创建了这样的映射 xml 文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="pl.hibernatePackage">

<class name="City" table="city">

<id name="id" column="ID" type="int">
<generator class="native"/>
</id>
<property name="name" column="Name" type="string"/>
<property name="countryCode" column="CountryCode" type="string"/>
<property name="district" column="District" type="string"/>
<property name="population" column="Population" type="int"/>

</class>

</hibernate-mapping>

City.java 如下所示:

public class City
{
private Integer id;
private String name;
private String countryCode;
private String district;
private Integer population;

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

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

public String getCountryCode() {
return countryCode;
}

public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}

public String getDistrict() {
return district;
}

public void setDistrict(String district) {
this.district = district;
}

public Integer getPopulation() {
return population;
}

public void setPopulation(Integer population) {
this.population = population;
}
}

我正在 HibernateUtil.java 中创建 session

public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();

StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();

return configuration.buildSessionFactory(serviceRegistry);
}
catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/world</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password">1234</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>

<!-- List of XML mapping files -->
<mapping resource="pl/hibernatePackage/City.hbm.xml"/>

</session-factory>
</hibernate-configuration>

主要

public class Main
{
public static void main(String[] args)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

List<City> cities = session.createQuery("from City").list();

for(City c : cities) {
System.out.println(c.getId() + "\t" + c.getName() + "\t" + c.getCountryCode() + "\t" + c.getDistrict() +
"\t" + c.getPopulation());
}

session.getTransaction().commit();

HibernateUtil.getSessionFactory().close();
}
}

编辑完整错误列表
enter image description here

编辑 2
javac 结果 enter image description here

最佳答案

我已经做了一些测试,并通过使用完全限定的类名使其工作:

session.createQuery("from pl.hibernatePackage.City")

现在这只是一个不影响您的配置的解决方法..

深入挖掘后,我发现自 hibernate 5.x 版本以来,构建 sessionFactory 的策略有所不同。

我通过如下实现 sessionFactory 使您的示例工作:

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure()
.build();

Metadata metadata = new MetadataSources( standardRegistry )
.getMetadataBuilder()
.build();

return configuration.buildSessionFactory(serviceRegistry);

这里用例子解释:jboss documentation (第 2.4 点)

关于java - MySQL 表未被 Hibernate 5+ 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42421806/

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