【发布时间】:2015-12-11 09:49:11
【问题描述】:
我正在尝试在 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 如下并更改了休眠文件以使用它。
<?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]
【问题讨论】:
-
你用错方言了..
-
是否有特定的方言可供访问?
-
这里 stackoverflow.com/questions/1749464/… 我找到了 SQLServerDialect 但它也不起作用
-
您可以使用 [link]HXTT(hxtt.com/hibernate.html) 的方言。但如果可能,请尝试使用其他数据库...
-
带注释的类(实体)的映射在哪里?遗憾的是,Hibernate 在启动时不会扫描带注释的类,因此您必须手动配置 Hibernate,通过 XML 配置文件或以编程方式,以便它知道您的带注释的类。
标签: java hibernate maven ucanaccess