2013-11-01 196 views
1

我想使用連接做一個非常簡單的JPQL,但我沒有成功。我想獲取用戶的最近登錄日期,並給出用戶標識。我正在使用Spring Data JPA + Hibernate。JPQL JOIN查詢

我有一個包含表活動MySQL數據庫:

@Table(name = "activity") 
@SuppressWarnings("serial") 
public class Activity implements Serializable { 
@Id 
@GeneratedValue 
private Long id; 

@Column(name = "date", insertable = true, nullable = false, updatable = false) 
@Temporal(TemporalType.TIMESTAMP) 
private Date date; 

@Column(insertable = true, name = "organization_id", nullable = true, updatable = true, unique = false) 
@Basic(optional = true) 
private Long organizationId; 

@Column(insertable = true, name = "operation", nullable = false, updatable = true, unique = false) 
@Basic(optional = false) 
private String operation; 

//getters and setters 
} 

它包含了許多其他表,包括LoginAttempt:

@Table(name = "login_attempt") 
@SuppressWarnings("serial") 
public class LoginAttempt implements Serializable{ 

@Id 
@GeneratedValue 
private Long id; 

@OneToOne(fetch = FetchType.EAGER, optional = false) 
@Basic(optional = false) 
@JoinColumn(name = "activity_id", insertable = true, nullable = false, unique = false, updatable = false) 
private Activity activity; 

@Column(insertable = true, name = "user_id", nullable = true, updatable = true, unique = false) 
@Basic(optional = true) 
private Long userId; 

@Column(insertable = true, name = "user_email", nullable = true, updatable = true, unique = false) 
@Basic(optional = true) 
private String userEmail; 

@Column(insertable = true, name = "login_successful", nullable = false, updatable = true, unique = false) 
@Basic(optional = false) 
private boolean loginSuccessful; 

//getters and setters 
} 

所有其它表使用一個名爲列引用活動「ACTIVITY_ID 」。

這是我的一個方法和@Query註釋的DAO。

public interface MyDAO extends JpaRepository<Activity, Long> { 

@Query("SELECT a FROM Activity a, LoginAttempt la JOIN a.id la WHERE la.user_email = (:userId) AND a.date = (SELECT MAX(a.date))") 
public Activity findMostRecentLoginForUser(@Param("userId") Long userId); 

} 

當我有一個簡單的選擇在@Query所有聲明它的作品,所以我連接到數據庫的正確和我能夠得到的一切回來。起初,我在查詢中遇到了很多驗證錯誤,但現在我得到以下(部分堆棧跟蹤)。

Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.domain.Activity com.dao.MyDAO.findMostRecentLoginForUser(java.lang.Long)! 
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:97) 
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:66) 
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.fromQueryAnnotation(SimpleJpaQuery.java:169) 
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:114) 
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:160) 
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68) 
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:290) 
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) 
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162) 
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44) 
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142) 
... 44 more 
Caused by: java.lang.NullPointerException 
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:393) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3645) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3431) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247) 
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:250) 
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185) 
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138) 
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105) 
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) 
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) 
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221) 
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199) 
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778) 
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366) 
at com.sun.proxy.$Proxy33.createQuery(Unknown Source) 
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:91) 
... 54 more 

我的大腦被炸了,我無法把我的腦袋繞過我讀過的任何例子。任何幫助表示讚賞。謝謝。

回答

0

您的查詢格式錯誤 - 它的子查詢未指定從選擇。另外,由於您有從LoginAttemptActivity的直接參考,您不需要進行任何明確的連接。試試類似的東西(未經測試):

SELECT la.activity FROM LoginAttempt la 
WHERE la.user_email = :userId 
AND la.activity.date = (SELECT MAX(la2.activity.date) FROM LoginAttempt la2 
         WHERE la2.user_email = :userId) 

子查詢選擇給定用戶的最近日期。外部查詢爲給定用戶選擇Activity,其日期是子查詢的結果。

+0

完美!當我意識到自己是一個白癡時,我實際上已經開始不加入任何聯合。感謝你的回答! :-) – tikka