2013-07-15 60 views
1

請幫助我,我認爲有些事情我沒有做到正確。 我有UserContact,這是Many-to-Many的關係。休眠多對多的數據檢索,通過查詢

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 
    . 
    . 

映射的用戶:

<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> 

映射的聯繫:

<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> 

我想編寫一個查詢來獲得與User相關的Contacts已登錄

當我嘗試編寫以下查詢:

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(); 

我收到以下錯誤:

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] 

我知道錯誤消息的含義,但我無法弄清楚如何編寫此查詢。

首先,我做了這樣的:Hibernate many-to-many data retrieval

,但現在我寫一個查詢,因爲我需要檢查的Contact狀態等參數。

感謝 Harbir

這是我如何打電話查詢:

@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; 
    } 

以下是堆棧跟蹤:

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[email protected]]: 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 [[email protected]]: 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 [[email protected]]: 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 
+0

你是什麼意思'檢查聯繫狀態'? – Atropo

+0

通過'檢查聯繫人狀態'我的意思是,我需要根據他們的狀態獲取聯繫人。我知道我們可以通過查詢或標準來做。我試圖做與查詢和它不工作。 – Harbir

+0

我不明白如何獲取此錯誤消息甚至可能,因爲您的查詢不嘗試訪問聯繫人屬性userId。你確定你部署了正確的代碼,並粘貼了確切的錯誤信息嗎? –

回答

0

讓我們來看看,如果我理解正確的。您想要「獲取與登錄的用戶關聯的聯繫人」。 「我需要檢查聯繫人狀態...」,對嗎?

如果是這種情況,請嘗試以其他方式編寫查詢。所以,你可以從你有here的查詢開始。假設您希望「所有登錄用戶的狀態爲'ACTIVE'的聯繫人」。

​​

我希望它可以幫助...

=== UPDATE1 ===

你說得對,我想用戶將有一些接觸。怎麼樣,如果你嘗試類似:

"FROM com.smallworks.model.User as u LEFT JOIN u.contacts as c WHERE u.userId=:userId AND c.status=:status" 

注意,你可以開始與你的聲明「FROM」,而不是「選擇」。請參閱tutorial中的一些很好的示例。

另一個重要的參考:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

此外,請注意,在你的情況下,類用戶擁有的關係,有些語句需要採取考慮到所有權。

+0

我認爲,我可以這樣做,但這樣我將首先獲取所有聯繫人並檢查我需要哪些聯繫人。 如果我們使用Criteria或Query來獲取我們需要的數據,而不是獲取所有數據然後檢查,那麼您認爲它不會更快嗎? – Harbir

+0

我試過以下內容 Query query = session.createQuery(「FROM com.smallworks.model.User as u LEFT JOIN u.contactSet as c WHERE u。用戶ID =:用戶ID 「); \t \t query.setParameter(」 用戶ID」,user.getUserID()); \t \t contactList = query.list(); 但這樣一來我得到的對象,而不是聯繫人列表當我嘗試遍歷列表時,它對投射抱怨。任何想法? – Harbir

+0

但是行數正確。 – Harbir

2

在XML映射聯繫,USERSET的映射是完全錯誤的:

<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> 

它應該是:從用戶

<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> 

它就像你看起來對我來說複製和粘貼此然後沒有更新它。

爲什麼使用XML而不是註釋?正如你所看到的,XML更容易出錯。另外,很多這些設置應該由Hibernate正確地默認。如果你剛剛遺漏了一些這些屬性,它可能會工作得很好。

+0

謝謝你這個湯姆,我會嘗試這些變化。 – Harbir