Hibernate - 使用net.ucanaccess.jdbc.UcanaccessDriver时的org.hibernate.MappingException

时间:2015-12-11 09:49:12

标签: java hibernate maven ucanaccess

我试图在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;
}

}

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

  ....
  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作为folow并更改了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]

2 个答案:

答案 0 :(得分:1)

异常表示您的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>

答案 1 :(得分:1)

hibernate在没有给出包名的情况下找不到类。我在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>