gpt4 book ai didi

java - 没有 XML 的 Hibernate ORM

转载 作者:行者123 更新时间:2023-12-01 13:58:13 24 4
gpt4 key购买 nike

到目前为止,我一直使用 EclipseLink,但现在我正在尝试使用 Hibernate 4 的 Java SE 项目。我尝试执行 NamedQuery,但出现以下异常:

HibernateLog --> 18:05:03 ERROR org.hibernate.internal.SessionFactoryImpl - HHH000177: Error in named query: SubCategory.findAll
org.hibernate.hql.internal.ast.QuerySyntaxException: Subcategory is not mapped [SELECT s from Subcategory s]

这是我的 DBUtils 类

public class DBUtils {

static final Logger logger = Logger.getLogger(DBUtils.class);

private static final ServiceRegistry serviceRegistry;
private static final SessionFactory sessionFactory;

/**
*
*/
//initialize session factory;

static {
try {
Configuration conf = new Configuration()
// mapped classes;
.addAnnotatedClass(Category.class)
.addAnnotatedClass(SubCategory.class)
.configure();

//.....

serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
sessionFactory = conf.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
logger.error("Initial SessionFactory creation failed");
System.err.println("." + ex);
throw new ExceptionInInitializerError(ex);
}
}


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

这是我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- Ommitted... -->

<!-- JDBC connection pool, use Hibernate internal connection pool -->
<property name="connection.pool_size">5</property>


<!-- Defines the SQL dialect used in Hibernate application -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>


<!-- hibernate sql output -->
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>

<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.CharSet">utf8</property>
</session-factory>
</hibernate-configuration>

最后我的实体都用@Entity("tableName")注释进行注释

我做错了什么?

最佳答案

Hibernate 区分大小写,并使用类名在其 HQL 中映射实体。所以

SELECT s from Subcategory s

将引用名为Subcategory的实体类。这是错误的。使用实际的类名

SELECT s from SubCategory s

因为您的类名为SubCategory,见

.addAnnotatedClass(SubCategory.class)

关于java - 没有 XML 的 Hibernate ORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19498531/

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