我有一个像下面这样的SQL查询,
SELECT patient.*,
contact.telephone
from patient
INNER JOIN contact ON patient.`idPatient` = contact.`idPatient`
这些是我的作品,
Patient.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 1:55:08 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="db.Patient" table="patient" catalog="example_hibernate" optimistic-lock="version">
<id name="idPatient" type="java.lang.Integer">
<column name="idPatient" />
<generator class="identity" />
</id>
<property name="title" type="string">
<column name="title" length="45" />
</property>
<property name="firstName" type="string">
<column name="firstName" length="45" />
</property>
<property name="lastName" type="string">
<column name="lastName" length="45" />
</property>
<property name="middleName" type="string">
<column name="middleName" length="45" />
</property>
<property name="dob" type="date">
<column name="dob" length="10" />
</property>
<property name="martitalStatus" type="java.lang.Boolean">
<column name="martitalStatus" />
</property>
<property name="gender" type="string">
<column name="gender" length="45" />
</property>
<property name="nic" type="string">
<column name="nic" length="45" />
</property>
<property name="dateCreated" type="timestamp">
<column name="dateCreated" length="19" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="lastUpdated" length="19" not-null="true" />
</property>
<set name="contacts" table="contact" inverse="true" lazy="true" fetch="select">
<key>
<column name="idPatient" not-null="true" />
</key>
<one-to-many class="db.Contact" />
</set>
</class>
</hibernate-mapping>
Contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 1:55:08 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="db.Contact" table="contact" catalog="example_hibernate" optimistic-lock="version">
<id name="idContact" type="java.lang.Integer">
<column name="idContact" />
<generator class="identity" />
</id>
<many-to-one name="patient" class="db.Patient" fetch="select">
<column name="idPatient" not-null="true" />
</many-to-one>
<property name="telephone" type="string">
<column name="telephone" length="45" />
</property>
<property name="address" type="string">
<column name="address" length="45" />
</property>
</class>
</hibernate-mapping>
然后我使用上面的sql,
SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();
Session session = sessionFactory.openSession();
List<Object[]> africanContinents = session.createQuery(
"SELECT patient.*,\n"
+ "contact.telephone\n"
+ "from patient\n"
+ "INNER JOIN contact ON patient.`idPatient` = contact.`idPatient`")
.list();
HibernateUtil.shutdown();
但是这个例子让我失意
org.hibernate.QueryException: unexpected char: '`' [SELECT patient.*,
contact.telephone
from patient
INNER JOIN contact ON patient.`idPatient` = contact.`idPatient`]
我不知道如何处理这个异常。因为这个sql查询是正确的。 对此例外有任何想法吗?
答案 0 :(得分:1)
createQuery中的查询应该用HQL或JPQL编写。
因此,您可以在HQL中重写您的查询(例如,从患者内部加入联系人那里选择p,c.telephone&#39;)请参阅文档here
或者您可以通过调用session.createSQLQuery而不是session.createQuery
来使用SQL查询希望这会有所帮助。 的Sebastien