Custom Query Spring Data JPA - java

I need a custom Query in my Repository which extends CrudRepository
#Repository
public interface SongListRepository extends CrudRepository<SongList, Integer> {
#Query("SELECT sl FROM song_list sl WHERE sl.owner=:owner")
Set<SongList> findAllSongListsForUser(
#Param("owner") String owner);
}
When I try to start the following excpetion is thrown:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'songListController': Unsatisfied dependency expressed through field 'songListService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'songListService': Unsatisfied dependency expressed through field 'repository';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'songListRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.Set htwb.ai.songsservice.repository.SongListRepository.findAllSongListsForUser(java.lang.String)!
How do i create custom Querys in a CrudRepository?

You don't need to create your own query. Simply use the auto generate query based on the method signature.
List<SongList> findAllByOwner(String owner);
This will create the query.

Related

DHIS-2 migration and merging

I am migrating and merging DHIS2 from 2.35.8 to 2.38.1.1. The 2.35.8 have some customization code on the core and I was successfully able to merge and create a war file. The problem comes when deploying and running the war file. The errors are as follows
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcConfig': Unsatisfied dependency expressed through field 'userSettingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.hisp.dhis.user.UserSettingService' defined in URL [jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%209.0/webapps/dhis/WEB-INF/lib/dhis-service-core-2.38.1.1.jar!/org/hisp/dhis/user/DefaultUserSettingService.class]: Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.hisp.dhis.user.UserService' defined in URL [jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%209.0/webapps/dhis/WEB-INF/lib/dhis-service-core-2.38.1.1.jar!/org/hisp/dhis/user/DefaultUserService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.hisp.dhis.user.UserStore' defined in URL [jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%209.0/webapps/dhis/WEB-INF/lib/dhis-service-core-2.38.1.1.jar!/org/hisp/dhis/user/hibernate/HibernateUserStore.class]: Unsatisfied dependency expressed through constructor parameter 5; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.hisp.dhis.security.acl.AclService' defined in URL [jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%209.0/webapps/dhis/WEB-INF/lib/dhis-service-acl-2.38.1.1.jar!/org/hisp/dhis/security/acl/DefaultAclService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.hisp.dhis.schema.SchemaService' defined in URL [jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%209.0/webapps/dhis/WEB-INF/lib/dhis-service-schema-2.38.1.1.jar!/org/hisp/dhis/schema/DefaultSchemaService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hisp.dhis.schema.DefaultSchemaService]: Constructor threw exception; nested exception is java.lang.UnsupportedOperationException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
Please help me on this How to tackle the problems

Error creating bean; Unsatisfied dependency expressed through field

I have a central library for certain functions and now I have trouble integrating that library.
The library is written in spring boot and contains a class: com.common.Security.
It is defined like this:
package com.common;
....
#Service
#EnableConfigurationProperties(SecurityProperties.class)
#Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Security {
....
}
I try to use this in another class:
package org.special;
import com.common.Security;
#Configuration
public class WebServiceConfig {
#Autowired
private Security security;
....
}
But I get some errors:
Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'WebServiceclient';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'webserviceClient':
Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'webServiceTemplate' defined in class path resource [org/special/WebServiceConfig.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.ws.client.core.WebServiceTemplate]: Factory method 'webServiceTemplate' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
nested exception is org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.Security': Scope 'request' is not active for the current thread;
consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found:
What can I do to fix this?
Removing the Scope was quite helpfull.
I tried this before but some of my tests failed after that. I didn't see, that this was because of missing settings in application.yaml for the tests.
They were not neccessary when scope is request.

Unsatisfied dependency in spring

I want to implement JPA repositories as mentioned here. I think my problem is I want to use some functions from the JPA Repository itself and I #Autorwired it But I'm not completely sure
Error Message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addBothStudentsHandler': Unsatisfied dependency expressed through method 'setMajorRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'majorRepository' defined in io.asiam.tansiq.repositories.MajorRepository defined in #EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot create inner bean '(inner bean)#78f146b5' of type [org.springframework.data.repository.core.support.RepositoryFragmentsFactoryBean] while setting bean property 'repositoryFragments'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#78f146b5': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customMajorRepositoryImplFragment': Cannot resolve reference to bean 'customMajorRepositoryImpl' while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customMajorRepositoryImpl': Unsatisfied dependency expressed through field 'majorRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'majorRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
What I have tried so far:
1- tried to use setter-based #Autowired instead of field-based (But no change)
2- tried to use #Lazy also nothing changed
**Code: **
CustomMajorRepository.java
public interface CustomMajorRepository {
void addStudentToMajor(Student student, Major major);
void increaseMajorLimit(UUID majorId, int amount);
}
CustomMajorRepositoryImpl.java
private MajorRepository majorRepository;
public MajorRepository getMajorRepository() {
return majorRepository;
}
#Autowired
public void setMajorRepository(MajorRepository majorRepository) {
this.majorRepository = majorRepository;
}
// rest of implementation details
MajorRepository.java
#Lazy
public interface MajorRepository extends JpaRepository<Major, UUID>, CustomMajorRepository {
}
I think you should use dependency injection via constructor or setter, for example:
private MajorRepository majorRepository;
public CostumMajorRepositoryImpl(MajorRepository mh) {
this.majorRepository = mh;
}
Here is the spring.io article about the setter and constructor injection:
https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/

Spring Boot ObjectMapper throws NullPointerException

I am currently working on an application that fails to run after upgrading to a newer snapshot version of a custom 3rd party dependency. Upon using this updated dependency I get the following error
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'listener': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'objectMapper' defined in class path resource [com/****/****/****/WebMvcConfig.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NullPointerException
The 'mapper' is what I am using locally, 'objectMapper' refers to a remote dependency. I realize this is may be challenging without code, but what could be causing this error? If I remove my objectmapper instance, the error only shifts to another section of the program, but the stack trace always leads to this:
Error creating bean with name 'objectMapper' defined in class path resource [com/****/****/****/WebMvcConfig.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NullPointerException
Ive tried different versions of com.fasterxml.jackson.core but that does not seem to help. Any suggestions at all would be appreciated.
Edit:
Additional Stack Trace:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cashModule' defined in file
[/Users/*****/******/*******/target/classes/*****/****/****/*****/cashModule.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'service5' defined in file
[/Users/****/******/******/target/classes/****/****/*****/*****/EventProcessingService.class]: Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventSource' defined in class path resource [com/****/****/****/******/EventConfig.class]:
Unsatisfied dependency expressed through method 'eventSource' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.stream.messaging.Source': Unsatisfied dependency expressed through field 'bindingTargetFactories'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'channelFactory' defined in class path resource [org/springframework/cloud/stream/config/BinderFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'channelFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'compositeMessageChannelConfigurer' defined in class path resource [org/springframework/cloud/stream/config/BinderFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'compositeMessageChannelConfigurer' parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'messageConverterConfigurer' defined in class path resource
[org/springframework/cloud/stream/config/BinderFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'messageConverterConfigurer' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compositeMessageConverterFactory' defined in class path resource [org/springframework/cloud/stream/config/ContentTypeConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.stream.converter.CompositeMessageConverterFactory]: Factory method 'compositeMessageConverterFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapper' defined in class path resource
[com/****/****/*****/WebMvcConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NullPointerException

BasicDataSource is overridden by non-compatible bean instance of type UrlDecodingDataSource

I tried to expose my Spring Restful web api to Pivotal Cloud Foundary, I successfully connected with Pivotal MySql database, in Local it is working fine but when I push the war in PCF it throws an exception
nested exception is java.lang.IllegalStateException: #Bean method
SpringBeanContainer.dataSource called as bean reference for type
[org.apache.commons.dbcp2.BasicDataSource] but overridden by non-compatible
bean instance of type [org.cloudfoundry.reconfiguration.org.springframework.cloud.service.relational.UrlDecodingDataSource].
I am new to cloud and i don't have an idea much about it, it seems that UrlDecodingDataSource tries to override something, due to this my sessionFactory is not able to create as bean.
Detailed Log
[OUT] org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'loginController': Unsatisfied dependency
expressed through field 'userDetailService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userDetailService': Unsatisfied dependency
expressed through field 'uddDao'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'userDetailDaoImpl': Unsatisfied dependency expressed
through field 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'sessionFactory' defined in
com.javarnd.cc.configuration.SpringBeanContainer: Bean instantiation via
factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Factory method
'sessionFactory' threw exception; nested exception is
java.lang.IllegalStateException: #Bean method SpringBeanContainer.dataSource
called as bean reference for type [org.apache.commons.dbcp2.BasicDataSource]
but overridden by non-compatible bean instance of type [org.cloudfoundry.reconfiguration.org.springframework.cloud.service.relational.UrlDecodingDataSource].
DataSource snippet in my SpringBeanContainer
#Bean // Simple Bean Defination in XML
public BasicDataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty(DRIVER_CLASS));
dataSource.setUrl(env.getProperty(URL));
dataSource.setUsername(env.getProperty(USERNAME));
dataSource.setPassword(env.getProperty(PASSWORD));
return dataSource;
}
Kindly help me to resolve this or sugge

Categories

Resources