<hibernate-mapping package="entity.associative">
<class name="UserGroup" table="user_group">
<meta attribute="class-description">
This class contains user group information.
</meta>
<composite-id>
<key-property name="userID" column="u_id"/>
<key-property name="groupID" column="g_id"/>
</composite-id>
<property name="hours" type="double">
<column name="hours"/>
</property>
</class>
</hibernate-mapping>
<hibernate-mapping package="entity.user">
<class name="User" table="user">
<meta attribute="class-description">
This class contains user information.
</meta>
<id name="userID" type="string">
<column name="u_id" length="50"/>
</id>
<property name="fName" type="string">
<column name="f_name" length="100"/>
</property>
.
.
.
</hibernate-mapping>
我收到了错误
org.hibernate.hql.internal.ast.QuerySyntaxException:
Path expected for join!
[FROM entity.user.User u INNER JOIN UserGroup ug
where u.userID = ug.userID and ug.groupID = 'EORG']
最有可能的原因是它们位于不同的包中 - 一个位于entity.user
,另一个位于entity.associative
。有没有办法改变我的查询?
我目前的查询是:
tx = session.beginTransaction();
String queryStr = "FROM User u INNER JOIN UserGroup ug where u.userID = ug.userID and ug.groupID = 'EORG'";
Query query = session.createQuery(queryStr);
userList = (ArrayList<User>) query.list();
答案 0 :(得分:0)
在您的查询中,您必须使用以下显式包:
tx = session.beginTransaction();
String queryStr = "FROM entity.user.User u,
entity.associative.UserGroup ug
where u.userID = ug.userID and ug.groupID = 'EORG'";
Query query = session.createQuery(queryStr);
userList = (ArrayList) query.list();
通常为了防止这种行为,我写这样的查询:
String myQuery = " FROM " + User.class.getName() + " u, " +
UserGroup.class.getName() + " ug
where u.userID = ug.userID and ug.groupID = 'EORG'";
使用getName()可以获得类的完整路径
答案 1 :(得分:0)
要加入User
和UserGroup
,您需要在User
至UserGroup
class User {
@OneToOne
private UserGroup userGroup;
}
FROM User u INNER JOIN u.userGroup ug where ug.groupID = 'EORG'
此查询只是一个示例。它可以用更简单的形式重写
FROM User u where u.userGroup.groupID = 'EORG'
Hibernate的最新版本能够使用没有这种关联的连接,但需要使用on
子句。