本文介绍了通过查询休眠多对多数据检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,我认为有些事情我没有做到正确。
我有用户和联系人,它们位于多对多关系。



User.java:

  public class User {

private Integer userID;

私人字符串userLoginEmail;

私人套餐<联络人> contactSet = new HashSet< Contact>();

public User(){

}


获得者和设定者

Contact.java

  public class Contact实现Serializable {
private Integer contactID;
私人设定<使用者> userSet = new HashSet< User>();
public Contact(){
}

获得者和设定者


用户映射:

 < hibernate-mapping package =com.smallworks.modelschema =projectdb> 
< class name =Usertable =USERACCOUNT>
< id column =USER_IDlength =500name =userID>
< generator class =increment/>
< / id>
< set cascade =allfetch =selectlazy =truename =contactSet
sort =unsortedtable =USER_CONTACT>
< key column =USER_ID/>
column =CONTACT_IDorder-by =CONTACT_IDunique =false/>
< / set>
< / class>
< / hibernate-mapping>

联系人映射:

 < hibernate-mapping package =com.smallworks.modelschema =projectdb> 
< class name =联系人表格=联系人>
< id column =CONTACT_IDlength =500name =contactID>
< generator class =increment/>
< / id>
<! - 通过User_Contact表与用户进行多对多映射 - >
< set inverse =truelazy =falsename =userSetsort =unsortedtable =USER_CONTACT>
< key column =USER_ID/>
< / set>
< / class>
< / hibernate-mapping>

我试图编写一个查询来获取联系人>与已登录的用户相关联。当我尝试写入以下查询时: / p>

 查询查询= session.createQuery(从com.src.model.Contact中选择联系人联系人联系人contact.userSet你在哪里?用户ID =:用户ID); 
query.setParameter(userID,user.getUserID());
contactList = query.list();

我得到以下错误:

  org.hibernate.QueryException:无法解析属性:userID:com.src.model.Contact [从com.src.model.Contact中选择联系人联系人联系人contact.userSet u其中u。 userID =:userID] 

我知道错误信息的含义,但我无法计算出来如何写这个查询。



首先,我这样做:

但现在我在写一个查询,因为我需要检查联系状态和其他参数。



感谢
Harbir



这是我如何调用查询:

  @Override 
public List< Contact> getContactList(用户用户){
log.info(getContactList(User user));
List contactList = new ArrayList();
Session session = sessionFactory.getCurrentSession();
System.out.println(关于查询);
查询查询= session.createQuery(从com.smallworks.model.Contact联系人加入contact.userSet您选择联系人,其中u.userID =:userID);
query.setParameter(userID,user.getUserID());
contactList = query.list();
返回contactList;
}

以下是堆栈跟踪:

 关于查询
18:56:01.011 [http-bio-8080-exec-6] DEBUG ohhql.ast.QueryTranslatorImpl - parse( ) - HQL:从com.smallworks.model中选择联系人。联系人联系人联系人contact.userSet u其中u.userID =:userID
18:56:01.020 [http-bio-8080-exec-6] DEBUG org。 hibernate.hql.ast.AST - --- HQL AST ---
\- [QUERY]'query'
+ - [SELECT_FROM]'SELECT_FROM'
| +'从[
|]中获得[FROM] | + - [范围]'范围'
| | | + - [DOT]'。'
| | | | + - [DOT]'。'
| | | | | + - [DOT]'。'
| | | | | | + - [IDENT]'com'
| | | | | | \- [IDENT]'smallworks'
| | | | | \- [IDENT]'model'
| | | | \- [IDENT]'联系'
| | | \- [别名]'联系'
| | \- [JOIN]'加入'
| | + - [DOT]'。'
| | | + - [IDENT]'contact'
| | | \- [IDENT]'userSet'
| | \- [别名]'你'
| \- [SELECT]'选择'
| \- [IDENT]'contact'
\- [WHERE]'其中'
\- [EQ]'='
+ - [DOT]'。
| + - [IDENT]'u'
| \ [IDENT]'userID'
\- [COLON]':'
\- [IDENT]'userID'

18:56:01.020 [http -bio-8080-exec-6] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException():没有错误
18:56:01.041 [http-bio-8080-exec-6] DEBUG ohhql。 antlr.HqlSqlBaseWalker - 选择<< begin [level = 1,statement = select]
18:56:01.050 [http-bio-8080-exec-6] DEBUG o.hibernate.hql.ast.tree.FromElement - FromClause {level = 1}: com.smallworks.model.Contact(contact) - > contact0_
18:56:01.051 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromReferenceNode - 已解决:contact - > contact0_.CONTACT_ID
18:56:01.053 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.tree.DotNode - getDataType():userSet - > org.hibernate.type.SetType(com.smallworks.model.Contact.userSet)
18:56:01.053 [http-bio-8080-exec-6] DEBUG ohhql.ast.tree.FromElementFactory - createManyToMany( ):path = contact.userSet role = com.smallworks.model.Contact.userSet associatedEntityName = com.smallworks.model.Contact
18:56:01.055 [http-bio-8080-exec-6] DEBUG o。 hibernate.hql.ast.tree.FromElement - FromClause {level = 1}:com.smallworks.model.Contact(u) - > contact2_
18:56:01.055 [http-bio-8080-exec-6] DEBUG o.hibernate.hql.ast.tree.FromClause - addJoinByPathMap():contact.userSet - > smallworksdb.CONTACT contact2_
18:56:01.055 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.tree.DotNode - dereferenceCollection():为contact.userSet创建新的FROM元素: smallworksdb.USER_CONTACT contact2_
18:56:01.055 [http-bio-8080-exec-6] DEBUG ohhql.ast.tree.FromReferenceNode - 已解决:contact.userSet - > 。
18:56:01.056 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.HqlSqlWalker - createFromJoinElement(): - 连接树 -
\\ [JOIN_FRAGMENT ] FromElement:'smallworksdb.USER_CONTACT contact2_'FromElement {显式,不是集合连接,不是读取连接,获取非惰性属性,classAlias = u,role = com.smallworks.model.Contact.userSet,tableName = smallworksdb.CONTACT ,tableAlias = contact2_,origin = smallworksdb.CONTACT contact0_,colums = {contact0_.CONTACT_ID,className = com.smallworks.model.Contact}}

18:56:01.056 [http-bio-8080- exec-6] DEBUG ohhql.ast.tree.FromReferenceNode - 已解决:contact - > contact0_.CONTACT_ID
18:56:01.060 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromReferenceNode - 已解决:u - > contact2_.CONTACT_ID
18:56:01.060 [http-bio-8080-exec-6]调试oswsmaAnnotationMethodHandlerExceptionResolver - 解决处理异常[com.smallworks.controller.ProjectController@1f183871]:org.hibernate.QueryException:可能未解析属性:userID:com.smallworks.model.Contact [从com.smallworks.model中选择联系人。联系人联系人联系人.userSet u其中u.userID =:userID]
18:56:01.062 [http -bio-8080-EXEC-6] DEBUG oswsmaResponseStatusExceptionResolver - 解决从处理器[com.smallworks.controller.ProjectController@1f183871]例外:org.hibernate.QueryException:无法解析属性:用户ID的:com.smallworks.model.Contact [选择联系人从com.smallworks.model.Contact联系人加入contact.userSet u其中u.userID =:userID]
18:56:01.062 [http-bio-8080-exec-6] DEBUG oswsmsDefaultHandlerExceptionResolver - 解析来自处理程序的异常[com.smallworks.controller.ProjectController@1f183871]:org.hibernate.Qu redException:无法解析属性:userID:com.smallworks.model.Contact [从com.smallworks.model中选择联系人。联系人联系人contact.userSet u其中u.userID =:userID]
18:56: 01.069 [http-bio-8080-exec-6] DEBUG osweb.servlet.DispatcherServlet - 无法完成请求
org.hibernate.QueryException:无法解析属性:userID:com.smallworks.model.Contact [从com.smallworks.model中选择联系人。联系人联系人联系人contact.userSet u其中u.userID =:userID]
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)[hibernate- core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:61)[hibernate-core-3.3.1.GA。 jar:3.3.1.GA]
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1385)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.persister.collec tion.AbstractCollectionPersister.toType(AbstractCollectionPersister.java:1457)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType( FromElementType.java:302)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:407) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:589)[hibernate-core-3.3 .1.GA.jar:3.3.1.GA]
在org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:264)[hibernate-core-3.3.1.GA.jar :3.3.1.GA]
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:211)[hibernate-core-3.3.1.GA.jar:3.3.1.GA ]
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:750)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.hql.antlr。 HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java: 4041)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3525)[hibernate-core-3.3 .1.GA.jar:3.3.1.GA]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)[hibernate-core-3.3.1.GA.jar:3.3 .1.GA]
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.hql。 antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)[hibernate-core -3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)[hibernate-core-3.3.1.GA.jar :3.3.1.GA]
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate。 hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.engine.query.HQLQueryPlan。< init>(HQLQueryPlan.java:101)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.engine.query.HQLQueryPlan。< init>(HQLQueryPlan。 java:80)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
在org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)[hibernate-core -3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan (AbstractSessionImpl.java:156)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)[hibernate- core-3.3.1.GA.jar:3.3.1.GA]
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)[hibernate-core-3.3.1.GA.jar: 3.3.1.GA]
at com.smallworks.dao.impl.ContactDAOImpl.getContactList(ContactDAOImpl.java:92)[ContactDAOImpl.class:na]
at com.smallworks.service.impl.ContactServiceImpl .getContactList(ContactServiceImpl.java:95)[ContactServiceImpl.class:na]
at com.smallworks.controller.ProjectController.addContact(ProjectController.java:117)[ProjectController.class:na]
at sun .reflect.NativeMethodAccessorImpl.invoke0(Native Method)[na:1.6.0_33]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)[na:1.6.0_33]
at sun.reflect.DelegatingMethodAccessorImpl .invoke(Unknown Source)[na:1.6.0_33]
at java.lang.reflect.Method.invoke(Unk nown源)[NA:1.6.0_33]在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)[弹簧网络3.0.5.RELEASE.jar
: 3.0.5.RELEASE]
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)[spring-webmvc-3.0.5.RELEASE.jar:3.0.5。发布]
在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)[spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
。在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)[弹簧webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
。在org.springframework.web .servlet.DispatcherServlet.doService(DispatcherServlet.java:719)[spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet .java:644)[spring-web mvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)[spring-webmvc-3.0.5.RELEASE。 jar:3.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)[servlet-api.jar:na]
at javax.servlet.http.HttpServlet .service(HttpServlet.java:728)[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)[catalina.jar:7.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)[catalina.jar:7.0.33]
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal (OpenSessionInViewFilter.java:198)[弹簧ORM-3.0.5.RELEASE.jar:3.0.5.RELEASE]在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
[ spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
在org.apache.catalina.core.ApplicationFilte rChain.internalDoFilter(ApplicationFilterChain.java:243)[catalina.jar:7.0.33]
。在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)[catalina.jar:7.0.33 ]
。在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)[catalina.jar:7.0.33]
。在org.apache.catalina.core.StandardContextValve.invoke( StandardContextValve.java:123)[catalina.jar:7.0.33]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)[catalina.jar:7.0.33]
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)[catalina.jar:7.0.33]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java: 99)[catalina.jar:7.0.33]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)[catalina.jar:7.0.33]
at org。 apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)[catalina.jar:7 .0.33]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)[catalina.jar:7.0.33]
at org.apache.coyote.http11.AbstractHttp11Processor。 process(AbstractHttp11Processor.java:1004)[tomcat-coyote.jar:7.0.33]
at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)[tomcat-coyote.jar:7.0 .33]
at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)[tomcat-coyote.jar:7.0.33]
at java.util。 $ n $ b $ at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)[na:1.6.0_33]
at java.lang.Thread.run(Unknown Source)[na:1.6.0_33]
18:56:01.069 [http-bio-8080-exec-6] DEBUG osohsOpenSessionInViewFilter - 在OpenSessionInViewFilter中关闭单个Hibernate Session
18:56:01.069 [http-bio-8080-exec-6] DEBUG osohibernate3.SessionFactoryUtils - 关闭Hiberna te会话


解决方案

在XML映射Contact中,映射userSet完全错误:

 < hibernate-mapping package =com.smallworks.modelschema =projectdb> 
< class name =联系人表格=联系人>
<! - ... --->
< set inverse =truelazy =falsename =userSetsort =unsortedtable =USER_CONTACT>
< key column =USER_ID/>
< / set>
< / class>
< / hibernate-mapping>

应该是:

 < set inverse =truelazy =falsename =userSetsort =unsortedtable =USER_CONTACT> 
< key column =CONTACT_ID/>
< / set>

它看起来像你从用户复制并粘贴的,然后没有更新它。



为什么使用XML而不是注释?正如你所看到的,XML更容易出错。另外,很多这些设置应该由Hibernate正确地默认。如果您刚刚忽略了其中一些属性,它可能会正常工作。


Please help me, I think there is something that I am not doing correct.I have User and Contact, that are in Many-to-Many relation.

User.java:

public class User {

    private Integer userID;

    private String userLoginEmail;

    private Set<Contact> contactSet = new HashSet<Contact>();

    public User() {

    }

    .
    getters and setters
    .

Contact.java

public class Contact implements Serializable {
    private Integer contactID;
    private Set<User> userSet= new HashSet<User>();
    public Contact() {
    }
    .
    getters and setters
    .
    .

Mapping for User:

<hibernate-mapping package="com.smallworks.model" schema="projectdb">
 <class name="User" table="USERACCOUNT">
  <id column="USER_ID" length="500" name="userID">
   <generator class="increment"/>
  </id>
  <set cascade="all" fetch="select" lazy="true" name="contactSet"
   sort="unsorted" table="USER_CONTACT">
   <key column="USER_ID"/>
   <many-to-many class="com.smallworks.model.Contact"
    column="CONTACT_ID" order-by="CONTACT_ID" unique="false"/>
  </set>
  </class>
</hibernate-mapping>

Mapping for Contact:

<hibernate-mapping package="com.smallworks.model" schema="projectdb">
 <class name="Contact" table="CONTACT">
  <id column="CONTACT_ID" length="500" name="contactID">
   <generator class="increment"/>
  </id>
  <!-- many to many mapping with the User via User_Contact table -->
  <set inverse="true" lazy="false" name="userSet" sort="unsorted" table="USER_CONTACT">
    <key column="USER_ID"/>
    <many-to-many class="com.smallworks.model.Contact" column="CONTACT_ID" unique="false"/>
  </set>
 </class>
</hibernate-mapping>

I am trying to write a query to get the Contacts associated with the User that is logged in.

When I try to write the following query:

Query query=session.createQuery("select contact from com.src.model.Contact  contact join contact.userSet u where u.userID=:userID");
        query.setParameter("userID", user.getUserID());
        contactList=query.list();

I get the following error:

org.hibernate.QueryException: could not resolve property: userID of: com.src.model.Contact [select contact from com.src.model.Contact  contact join contact.userSet u where u.userID=:userID]

I know what the error message means, but I am not being able to figure out how to write this query.

First I did like this: Hibernate many-to-many data retrieval

but now I am writing a query because I need to check on the Contact status, and other parameters.

ThanksHarbir

This is how I am calling the query:

@Override
    public List<Contact> getContactList(User user) {
        log.info("getContactList(User user)");
        List contactList = new ArrayList();
        Session session = sessionFactory.getCurrentSession();
        System.out.println("About to hit the query");
        Query query=session.createQuery("select contact from com.smallworks.model.Contact  contact join contact.userSet u where u.userID=:userID");
        query.setParameter("userID", user.getUserID());
        contactList=query.list();
        return contactList;
    }

The following is the stack trace:

About to hit the query
18:56:01.011 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.QueryTranslatorImpl - parse() - HQL: select contact from com.smallworks.model.Contact  contact join contact.userSet u where u.userID=:userID
18:56:01.020 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.AST - --- HQL AST ---
 \-[QUERY] 'query'
    +-[SELECT_FROM] 'SELECT_FROM'
    |  +-[FROM] 'from'
    |  |  +-[RANGE] 'RANGE'
    |  |  |  +-[DOT] '.'
    |  |  |  |  +-[DOT] '.'
    |  |  |  |  |  +-[DOT] '.'
    |  |  |  |  |  |  +-[IDENT] 'com'
    |  |  |  |  |  |  \-[IDENT] 'smallworks'
    |  |  |  |  |  \-[IDENT] 'model'
    |  |  |  |  \-[IDENT] 'Contact'
    |  |  |  \-[ALIAS] 'contact'
    |  |  \-[JOIN] 'join'
    |  |     +-[DOT] '.'
    |  |     |  +-[IDENT] 'contact'
    |  |     |  \-[IDENT] 'userSet'
    |  |     \-[ALIAS] 'u'
    |  \-[SELECT] 'select'
    |     \-[IDENT] 'contact'
    \-[WHERE] 'where'
       \-[EQ] '='
          +-[DOT] '.'
          |  +-[IDENT] 'u'
          |  \-[IDENT] 'userID'
          \-[COLON] ':'
             \-[IDENT] 'userID'

18:56:01.020 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.ErrorCounter - throwQueryException() : no errors
18:56:01.041 [http-bio-8080-exec-6] DEBUG o.h.hql.antlr.HqlSqlBaseWalker - select << begin [level=1, statement=select]
18:56:01.050 [http-bio-8080-exec-6] DEBUG o.hibernate.hql.ast.tree.FromElement - FromClause{level=1} :  com.smallworks.model.Contact (contact) -> contact0_
18:56:01.051 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved :  contact -> contact0_.CONTACT_ID
18:56:01.053 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.tree.DotNode - getDataType() : userSet -> org.hibernate.type.SetType(com.smallworks.model.Contact.userSet)
18:56:01.053 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromElementFactory - createManyToMany() : path = contact.userSet role = com.smallworks.model.Contact.userSet associatedEntityName = com.smallworks.model.Contact
18:56:01.055 [http-bio-8080-exec-6] DEBUG o.hibernate.hql.ast.tree.FromElement - FromClause{level=1} :  com.smallworks.model.Contact (u) -> contact2_
18:56:01.055 [http-bio-8080-exec-6] DEBUG o.hibernate.hql.ast.tree.FromClause - addJoinByPathMap() : contact.userSet -> smallworksdb.CONTACT contact2_
18:56:01.055 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.tree.DotNode - dereferenceCollection() : Created new FROM element for contact.userSet : smallworksdb.USER_CONTACT contact2_
18:56:01.055 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved :  contact.userSet -> .
18:56:01.056 [http-bio-8080-exec-6] DEBUG org.hibernate.hql.ast.HqlSqlWalker - createFromJoinElement() : -- join tree --
 \-[JOIN_FRAGMENT] FromElement: 'smallworksdb.USER_CONTACT contact2_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=u,role=com.smallworks.model.Contact.userSet,tableName=smallworksdb.CONTACT,tableAlias=contact2_,origin=smallworksdb.CONTACT contact0_,colums={contact0_.CONTACT_ID ,className=com.smallworks.model.Contact}}

18:56:01.056 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved :  contact -> contact0_.CONTACT_ID
18:56:01.060 [http-bio-8080-exec-6] DEBUG o.h.hql.ast.tree.FromReferenceNode - Resolved :  u -> contact2_.CONTACT_ID
18:56:01.060 [http-bio-8080-exec-6] DEBUG o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.smallworks.controller.ProjectController@1f183871]: org.hibernate.QueryException: could not resolve property: userID of: com.smallworks.model.Contact [select contact from com.smallworks.model.Contact  contact join contact.userSet u where u.userID=:userID]
18:56:01.062 [http-bio-8080-exec-6] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [com.smallworks.controller.ProjectController@1f183871]: org.hibernate.QueryException: could not resolve property: userID of: com.smallworks.model.Contact [select contact from com.smallworks.model.Contact  contact join contact.userSet u where u.userID=:userID]
18:56:01.062 [http-bio-8080-exec-6] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [com.smallworks.controller.ProjectController@1f183871]: org.hibernate.QueryException: could not resolve property: userID of: com.smallworks.model.Contact [select contact from com.smallworks.model.Contact  contact join contact.userSet u where u.userID=:userID]
18:56:01.069 [http-bio-8080-exec-6] DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
org.hibernate.QueryException: could not resolve property: userID of: com.smallworks.model.Contact [select contact from com.smallworks.model.Contact  contact join contact.userSet u where u.userID=:userID]
    at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:61) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1385) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.persister.collection.AbstractCollectionPersister.toType(AbstractCollectionPersister.java:1457) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:302) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:407) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:589) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:264) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:211) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:750) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3525) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650) [hibernate-core-3.3.1.GA.jar:3.3.1.GA]
    at com.smallworks.dao.impl.ContactDAOImpl.getContactList(ContactDAOImpl.java:92) [ContactDAOImpl.class:na]
    at com.smallworks.service.impl.ContactServiceImpl.getContactList(ContactServiceImpl.java:95) [ContactServiceImpl.class:na]
    at com.smallworks.controller.ProjectController.addContact(ProjectController.java:117) [ProjectController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [na:1.6.0_33]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [na:1.6.0_33]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [na:1.6.0_33]
    at java.lang.reflect.Method.invoke(Unknown Source) [na:1.6.0_33]
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.33]
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198) [spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.33]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.33]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.33]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.33]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.33]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.33]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931) [catalina.jar:7.0.33]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.33]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.33]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) [tomcat-coyote.jar:7.0.33]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.33]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-coyote.jar:7.0.33]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [na:1.6.0_33]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.6.0_33]
    at java.lang.Thread.run(Unknown Source) [na:1.6.0_33]
18:56:01.069 [http-bio-8080-exec-6] DEBUG o.s.o.h.s.OpenSessionInViewFilter - Closing single Hibernate Session in OpenSessionInViewFilter
18:56:01.069 [http-bio-8080-exec-6] DEBUG o.s.o.hibernate3.SessionFactoryUtils - Closing Hibernate Session
解决方案

In the XML mapping Contact, the mapping of userSet is completely wrong:

<hibernate-mapping package="com.smallworks.model" schema="projectdb">
 <class name="Contact" table="CONTACT">
  <!-- ... --->
  <set inverse="true" lazy="false" name="userSet" sort="unsorted" table="USER_CONTACT">
    <key column="USER_ID"/>
    <many-to-many class="com.smallworks.model.Contact" column="CONTACT_ID" unique="false"/>
  </set>
 </class>
</hibernate-mapping>

It should be:

  <set inverse="true" lazy="false" name="userSet" sort="unsorted" table="USER_CONTACT">
    <key column="CONTACT_ID"/>
    <many-to-many class="com.smallworks.model.User" column="USER_ID" unique="false"/>
  </set>

It looks to me like you copy-and-pasted this from User and then didn't update it.

Why are you using XML rather than annotations? XML is much more error-prone, as you can see. In addition, a lot of these settings should be defaulted correctly by Hibernate. If you had just left out some of these attributes, it probably would have worked fine.

这篇关于通过查询休眠多对多数据检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:19
查看更多