gpt4 book ai didi

java - Hibernate - 使用 net.ucanaccess.jdbc.UcanaccessDriver 时出现 org.hibernate.MappingException

转载 作者:行者123 更新时间:2023-12-02 04:06:40 24 4
gpt4 key购买 nike

我正在尝试查询 java 8 中的访问数据库,并且我使用 maven。我使用 net.ucanaccess.jdbc.UcanaccessDriver 进行连接,下面是我的 hibernate.cfg.xml:

<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">
net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name="hibernate.connection.url">jdbc:ucanaccess://D:\\db\\QIDB-Access\\db.mdb</property>
<property name="hibernate.connection.username"> </property>
<property name="hibernate.connection.password"> </property>
<mapping resource="IndikatorProcessor.hbm.xml" />
</session-factory>
</hibernate-configuration>

我的带注释的类如下所示:

@Entity
@Table(name = "Indikator")
public class IndikatorProcessor {

@Id
@Column(name = "QI_ID")
private int qiId;

public int getQiId() {
return qiId;
}

public void setQiId(int qiId) {
this.qiId = qiId;
}

}

在另一个类中,我创建 session 并编写一个简单的查询:

  ....
public void listQIIndikator() {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List<?> indikators = session.createQuery("From IndikatorProcessor").list();
for (Iterator<?> iterator = indikators.iterator(); iterator.hasNext();) {
IndikatorProcessor indiaktor = (IndikatorProcessor) iterator.next();
System.out.println("Indikator ID = " + indiaktor.getQiId());
}
tx.commit();

我收到此错误:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: IndikatorProcessor is not mapped

我不确定是什么原因导致了错误,因为它已被映射!是查询还是使用 ucanaccess 驱动程序进行连接?

现在我添加了一个 IndikatorProcessor.hbm.xml 如下,并更改了 hibernate 文件以使用它。

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

<hibernate-mapping>
<class name="IndikatorProcessor" table="Indikator">
<meta attribute="class-description">
This class contains the Indikator detail.
</meta>

<id name="qiId" type="int" column="QI_ID"> <!-- <generator class="native"/> --> </id>

<!-- <property name="qiId" column="QI_ID" type="int"/> -->
<property name="fkQig" column= "FK_QIG"></property>

现在我收到这些错误:

Caused by: org.hibernate.MappingException: class IndikatorProcessor not found while looking for property: fkQig

Caused by:  org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable  to load class [IndikatorProcessor]

最佳答案

异常表明您的IndikatorProcessor未映射到SessionFactory中,因此请按如下方式进行映射:

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class"> net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name="hibernate.connection.url">jdbc:ucanaccess://D:\\db\\QIDB-Access\\db.mdb</property>
<property name="hibernate.connection.username"> </property>
<property name="hibernate.connection.password"> </property>
<mapping class="your.package.IndikatorProcessor"/>
</session-factory>
</hibernate-configuration>

关于java - Hibernate - 使用 net.ucanaccess.jdbc.UcanaccessDriver 时出现 org.hibernate.MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34220480/

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