0

I am trying to query an access db in java 8 and i use maven. I am using net.ucanaccess.jdbc.UcanaccessDriver for my connection and below is my 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>

My class with annotations looks like this:

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

}

In another class i create the session and write a simple query:

  ....
  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();

I get this error:

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

I am not sure what causes the error since it is mapped! Is it the query or using the ucanaccess driver for the connection?

Now i added a IndikatorProcessor.hbm.xml as folow and changed the hibernate file to use it.

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

Now i get these errors:

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

and

Caused by:  org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable  to load class [IndikatorProcessor]
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
Iman
  • 769
  • 1
  • 13
  • 51
  • You are using the wrong dialect.. – NewBee Developer Dec 11 '15 at 09:50
  • Is there a particular dialect for access? – Iman Dec 11 '15 at 09:56
  • 1
    Here http://stackoverflow.com/questions/1749464/how-can-i-use-hibernate-with-ms-access i found SQLServerDialect but it dose not work either – Iman Dec 11 '15 at 10:01
  • You can use dialect from [link]HXTT(http://www.hxtt.com/hibernate.html). But if at all possible, try using some other database... – NewBee Developer Dec 11 '15 at 10:03
  • Did u test a native query working? Are you sure annotations in IndikatorProcessor.java are processed since I didn't see property in hibernate.cfg.xml – Murthy Dec 11 '15 at 10:37
  • Do i need a mapping property, when i use annotaion? I thought mapping is to refer the mappin xml. But i do not have mapping XML. Just annotations – Iman Dec 11 '15 at 10:45
  • 1
    Where is your mappings for annotated class (Entity)? Sadly, Hibernate does not Scan for annotated classes at startup and so you have to configure Hibernate manually, either through an XML configuration file or Pro-grammatically, so that it knows about your annotated classes. – Mihir Dec 11 '15 at 11:16

2 Answers2

1

Exception says your IndikatorProcessor is not mapped in SessionFactory, so do a mapping as follows:

<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>
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
1

hibernate could not find the class without giving the package name. I added the package name in IndikatorProcessor.hbm.xml and it worked.

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

Iman
  • 769
  • 1
  • 13
  • 51