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