0

I am attempting to integrate the Gradle project here into a Maven project of mine. The file data-context.xml is used to configure Hibernate with Spring to generate the repositories used for authenticating User requests to the RESTful interface.

Everything is working, but when I attempt to deploy onto a tomcat server I see the following issue:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityContextFilter' defined in file [/var/lib/tomcat6/webapps/services/WEB-INF/classes/com/services/filter/SecurityContextFilter.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.services.user.UserRepository]: : Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.services.user.domain.User com.services.user.UserRepository.findBySession(java.lang.String)!; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.services.user.domain.User com.services.user.UserRepository.findBySession(java.lang.String)!
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4210) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526) [catalina-6.0.24.jar:na]
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905) [catalina-6.0.24.jar:na]
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740) [catalina-6.0.24.jar:na]
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500) [catalina-6.0.24.jar:na]
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277) [catalina-6.0.24.jar:na]
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321) [catalina-6.0.24.jar:na]
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:722) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.StandardService.start(StandardService.java:516) [catalina-6.0.24.jar:na]
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) [catalina-6.0.24.jar:na]
    at org.apache.catalina.startup.Catalina.start(Catalina.java:593) [catalina-6.0.24.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) [bootstrap-6.0.24.jar:na]
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) [bootstrap-6.0.24.jar:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.services.user.domain.User com.services.user.UserRepository.findBySession(java.lang.String)!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
... 39 common frames omitted
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.services.user.domain.User com.services.user.UserRepository.findBySession(java.lang.String)!
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:136) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:204) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:347) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ... 50 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.hibernate.internal.util.StringHelper.root(StringHelper.java:345) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1994) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:61) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1970) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.tree.FromElement.getIdentityColumns(FromElement.java:355) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.tree.IdentNode.resolveAsAlias(IdentNode.java:167) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.tree.IdentNode.resolve(IdentNode.java:104) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:126) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:121) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:959) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.propertyRef(HqlSqlBaseWalker.java:1146) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2253) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2194) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1476) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:573) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328) ~[hibernate-entitymanager-4.3.10.Final.jar:4.3.10.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at com.sun.proxy.$Proxy50.createQuery(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86) ~[spring-data-jpa-1.8.0.RELEASE.jar:na]
    ... 63 common frames omitted

I am inclined to think that this is caused by the data-context.xml not initialising properly and therefore making the query validation fail. Below are samples of my code, and the spring dependancies I have included in my pom.

Data-context.xml -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/integration/jdbc
    http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/repository
    http://www.springframework.org/schema/data/repository/spring-repository.xsd">

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="samplePersistenceUnit"/>
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>

</bean>

<jpa:repositories base-package="com.services.user" />
<!-- 
    the above shows the following error in Eclipse  :
    java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getEnvironment()
 -->

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
      id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    </property>
</bean>

<tx:annotation-driven/>
...
</beans>

Domain.User class -

@Entity
@Table(name="rest_user")
public class User extends BaseEntity {

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

private static final int HASH_ITERATIONS = 1000;
private String firstName;
private String lastName;
private String emailAddress;
private String hashedPassword;
private boolean isVerified;

@Enumerated(EnumType.STRING)
private Role role;

@OneToMany(mappedBy="user",
             targetEntity=VerificationToken.class,
             cascade= CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<VerificationToken> verificationTokens = new ArrayList<VerificationToken>();

@OneToOne(fetch = FetchType.LAZY,
        mappedBy = "user",
        cascade = CascadeType.ALL)
private AuthorizationToken authorizationToken;

Domain.AuthorizationToken class -

@Entity
@Table(name="rest_authorization_token")
public class AuthorizationToken extends AbstractPersistable<Long> {

private final static Integer DEFAULT_TIME_TO_LIVE_IN_SECONDS = (60 * 60 * 24 * 30); //30 Days

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@Column(length=36)
private String token;

private Date timeCreated;

private Date expirationDate;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "token")
private User user;

UserRepository class -

public interface UserRepository  extends JpaRepository<User, Long> {

User findByEmailAddress(String emailAddress);

@Query("select u from User u where uuid = ?")
User findByUuid(String uuid);

@Query("select u from User u where u in (select user from AuthorizationToken where lastUpdated < ?)")
List<User> findByExpiredSession(Date lastUpdated);

@Query("select u from User u where u = (select user from AuthorizationToken where token = ?)")
User findBySession(String token);

}

VerificationToken class -

@Entity
@Table(name = "rest_verification_token")
public class VerificationToken extends BaseEntity {

private static final int DEFAULT_EXPIRY_TIME_IN_MINS = 60 * 24; //24 hours

@Column(length=36)
private String token;

private Date expiryDate;

@Enumerated(EnumType.STRING)
private VerificationTokenType tokenType;

private boolean verified;

@ManyToOne
User user;

public VerificationToken() {
    super();
    this.token = UUID.randomUUID().toString();
    this.expiryDate = calculateExpiryDate(DEFAULT_EXPIRY_TIME_IN_MINS);
}

public VerificationToken(User user, VerificationTokenType tokenType, int expirationTimeInMinutes) {
    this();
    this.user = user;
    this.tokenType = tokenType;
    this.expiryDate = calculateExpiryDate(expirationTimeInMinutes);
}

pom.xml Spring dependancies - (I have made sure there are no conflicts)

pom.xml

a.hrdie
  • 716
  • 2
  • 14
  • 35
  • Please add the fullstracktrace instead of a snippet. – M. Deinum Jul 01 '15 at 07:55
  • updated with full stracktrace as seen in the tomcat logs. – a.hrdie Jul 01 '15 at 07:57
  • sorry, incorrect trace there. i will update in 1 minute. – a.hrdie Jul 01 '15 at 08:00
  • 1
    Isn't your query just borked and your config to complex? The following query should work without a subselect `select u from User u where u. authorizationToken.token=?`, the same applies to the one for the expired sessions. Not sure if you need the `@IdClass` but that is from my pov. Another query that would work `select at.user from AuthorizationToken at where at.token=?` no need for the join and again the same applies to the one for the sessions. – M. Deinum Jul 01 '15 at 08:06
  • What I also don't get is you use Spring Boot but are deploying. Can you show what your class extending `SpringBootServletInitializer` looks like. – M. Deinum Jul 01 '15 at 08:08
  • I added the IdClass to User as eclipse was complaining that I hadn't declared one. It is complex, I am trying to get to grips with Spring using this project without much luck as yet! The original Gradle project is booted using a gradle tomcatrun task , so I assume that is where the need for spring-boot comes from. I have removed from the dependencies without any issues, I will try your suggestion for the Query now. Thank you so much for your help – a.hrdie Jul 01 '15 at 08:13
  • The error i get without an IdClass is the following: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: This class [class com.bpc.services.user.domain.User] does not define an IdClass – a.hrdie Jul 01 '15 at 08:27
  • I guess that is due the fact you are using a composite key for the user, is that really necessary? Do you really need the `@Id` on the `authorizationToken` field? I also doubt you should be mixing `@Entity` and `@Embeddable` and are you really sure it has to be a bi-directional relationship (just because you can doesn't mean you have to). – M. Deinum Jul 01 '15 at 08:30
  • thank you for your helpful comments, unfortunately I am still very much a beginner at Hibernate annotations as well as Spring.. I have added a PK id field and removed the @id annotation from the other fields. now I am seeing this error A Foreign key has the wrong number of column. should be 2. I know I need to add the User PK field to the joincolumn names in AuthToken. But then I need to add a referencedColumnName for the token field which I am unsure on. How would you recommend I simplify this to remove the bi-directional relationships and composite key please? Thank you again – a.hrdie Jul 01 '15 at 10:50
  • updated the code samples above to my current configurations – a.hrdie Jul 01 '15 at 10:50
  • Why would you even need a composite key? I don't see that you just have a join to another table... – M. Deinum Jul 01 '15 at 10:51
  • I was already answering the question. You don't need an `id` field as that is already present in the `AbstractPersistable` entity. Check my answer for updated mappings and queries. – M. Deinum Jul 01 '15 at 11:06

1 Answers1

1

For starters I suggest fixing your entities, you have a basic one-to-one mapping but you are making it way to complex, also you don't need a composite primary key. Next your queries are also way to complex you don't need the join you can simply return the user field from the AuthorizationToken.

Entities

All of your entities are extending either directly or indirectly AbstractPersistable that class already has an id field. Adding an additional @Id isn't needed and will only confuse JPA as you now all of a sudden require a composite primary key. The solution is quite easy remove the custom id fields from the entities.

@Entity
@Table(name="rest_authorization_token")
public class AuthorizationToken extends AbstractPersistable<Long> {

    @Column(length=36)
    private String token;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private User user;

}

The AuthorizationToken isn't embeddable so remove the annotation. For the one-to-one mapping add the @PrimaryKeyJoinColumn annotation. The token field doesn't need to be an @Id there is already an primary key field in the AbstractPersistable.

On the User entity remove the @IdClass as that isn't needed for a simple mapping. If you don't want a bi-directional relationship remove the authorizationToken field from the User entity. If you want to use it, just remove the @Id.

@Entity 
@Table(name="rest_user")
public class User extends BaseEntity {

    @OneToOne(fetch = FetchType.LAZY,
            mappedBy = "user",
            cascade = CascadeType.ALL)
    private AuthorizationToken authorizationToken;

}

Note: I omitted the rest of the entities code as that remained unchanged.

Now that the entities are cleaned up, simplify the queries by just returning the user field from the AuthorizationToken.

public interface UserRepository extends JpaRepository<User, Long> {

    User findByEmailAddress(String emailAddress);

    User findByUuid(String uuid);

    @Query("select user from AuthorizationToken where lastUpdated < ?")
    List<User> findByExpiredSession(Date lastUpdated);

    @Query("select user from AuthorizationToken where token = ?")
    User findBySession(String token);

}

I also noticed that you had a @Query for the findByUuid that shouldn't be needed as Spring Data JPA can generate that query for you.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • thank you so much!! the code is much easier to understand with some explanation. you have been a great help – a.hrdie Jul 01 '15 at 11:27
  • It has fixed the AuthorizationToken error completely, and the entity is created sucessfully. Now it is stuck on the VerificationToken with a similar issue - org.hibernate.MappingException: property mapping has wrong number of columns: com.services.user.domain.VerificationToken.user, I had hoped to fix this with the same logic but no luck – a.hrdie Jul 01 '15 at 11:40
  • Add it to your question, to see if there is anything wrong with the mapping. A simple `@ManyToOne` on the `user` field in the `VerificationToken` should be enough. – M. Deinum Jul 01 '15 at 11:44
  • In the failing run, I realised i had left an id filed in the User field. When I remove this, eclipse error log tells me that there is no PK field for the entity, even though I am extending AbstractPersistable. The same is true for my VerficationToken class, if I remove a declared Id field, the entity is not valid in Eclipse – a.hrdie Jul 01 '15 at 11:53
  • Sometimes you should ignore errors in Eclipse (at times it is a bit sketchy at detecting errors). Just compile and run your application and see if it works (I would expect so). – M. Deinum Jul 01 '15 at 11:54
  • Nice!! I am still relying too much on IDE help i think. It is now working perfectly. Thank you for your patience – a.hrdie Jul 01 '15 at 12:00