休眠 - 使用 net.ucanaccess.jdbc.UcanaccessDriver 时 org.hibernate.MappingException

Hibernate - org.hibernate.MappingException when using net.ucanaccess.jdbc.UcanaccessDriver

我正在尝试查询 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>

我的带有注释的 class 看起来像这样:

@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;
}

}

在另一个 class 中,我创建了会话并编写了一个简单的查询:

  ....
  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 如下所示,并更改了休眠文件以使用它。

<?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>

hibernate 在不提供包名称的情况下无法找到 class。我在 IndikatorProcessor.hbm.xml 中添加了包名,它起作用了。

<hibernate-mapping>
 <class name="model.IndikatorProcessor" table="Indikator">
  <meta attribute="class-description">
     This class contains the Indikator detail. 
    </meta>
  <id name="qiId" type="int" column="QI_ID">  </id>

  <property name="fkQig" type ="int" column= "FK_QIG"></property>