user_id是用户表id的外键。我想在hql中使用内连接来加入这两个表。
我收到此错误。
线程中的异常" main" org.hibernate.hql.ast.QuerySyntaxException:意外令牌:在第1行第74列[select user_name,来自com.csc.project.user的消息e内部连接推文p在p.user_id = e.id其中e.user_name =:名称] 在org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31) 在org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24) 在org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59) 在org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258) 在org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157) 在org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111) 在org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:77) 在org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:56) 在org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) 在org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) 在org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623) 在com.csc.project.tweetManager.viewtweet(tweetManager.java:80) 在com.csc.project.tweetManager.main(tweetManager.java:103)
这是我的hibernate.cfg
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/twitter</property>
<property name="connection.user">root</property>
<property name="connection.password">root</property>
<property name="show_sql">true </property>
<property name="dialet">org.hibernate.dialect.MySQLDialect </property>
<property name="hbm2ddl.auto">update </property>
<mapping resource="registration.hbm.xml" />
<mapping resource="tweet.hbm.xml"/>
</session-factory>
</hibernate-configuration>
reigistration.hbm
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.csc.project.user" table="t1">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="increment"/>
</id>
<property name="user_name" column="user_name" type="string"/>
<property name="user_email" column="user_email" type="string"/>
<property name="password" column="password" type="string"/>
<property name="created" type="timestamp" column="created"/>
</class>
</hibernate-mapping>
tweet.hbm
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.csc.project.tweet" table="t2">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="tweet_id" type="int" column="tweet_id">
<generator class="increment"/>
</id>
<property name="user_id" column="user_id" type="int"/>
<property name="message" column="message" type="string"/>
<!-- <property name="user_email" column="user_email" type="string"/>
<property name="password" column="password" type="string"/> -->
<property name="created" type="timestamp" column="created"/>
</class>
</hibernate-mapping>
答案 0 :(得分:0)
我认为你的bean属性是DB字段的名称
如果您想获得关于一个用户的推文,您的HQL将是:
select e.user_name, p.message
from tweet p, t.com.csc.project.user e
where p.user_id=e.id
and e.user_name=:name
在字段之前使用始终别名,您可以使用隐式连接(使用逗号)替换显式连接