Spring Boot w/ JPA: move #Entity to different package - spring

I'm having trouble with my first steps using Spring-Boot with JPA. I've started with a pretty minimalistic example from Git using Gradle.
Now simply moving Customer to another package, let's say to hello2 results in an exception Caused by: java.lang.IllegalArgumentException: Not an managed type: class hello2.Customer. I tried to add
#ComponentScan(basePackageClasses= {Customer.class}) // AND OR
#EnableJpaRepositories(basePackageClasses= {Customer.class})
to Application, but without success.
What am I doing wrong?

Location of entities in Spring Boot can be configured using #EntityScan.
By default, #EnableAutoConfiguration enables entity scanning in the package where it's placed (if it's not a default package).

You must locate entities and repositories pakages by using
#EnableJpaRepositories(basePackages = "your.repositories.pakage")
#EntityScan(basePackages = "your.entities.pakage")

this is what worked for me :
#EnableJpaRepositories(basePackages ={ "package1","package2"})
#EntityScan(basePackages ={ "package3","package4"})

Giving same package location (i.e base package) for below annotation worked for me :-
#SpringBootApplication(scanBasePackages = {"org.ashu.java.*"})
#EnableJpaRepositories(basePackages ={ "org.ashu.java.*"})
#EntityScan(basePackages ={ "org.ashu.java.*"})

Related

Spring boot Jpa is not working with Spring batch and spring integration

I am working with spring batch. I needed to add some jpa repositories. So previously i was using JDBCTemplate which was working fine.
But when I started working with JPA, the spring boot application could not find the repos. Which were there.
#Autowired
ClassLevelConfigRepo clcr;
I checked these things as the best practices.
Added #EnableJpaRepositories in springBoot application class.
Added #Repostiories to the repository interfaces.
extended the interfaces with JpaRepository<Account, String>
Added #Entity to the entity classes and defined the #Table and # Column annotations properly.
But I am still getting below error.
Field clcr in com.cloudtask.batchconfig.util.LhmUtility required a bean of type 'com.cloudtask.batchconfig.repo.ClassLevelConfigRepo' that could not be found.
I tried checking all the dependencies in pom.xml it was as per recommended. And I have all the tables defined properly in data base.
I was expecting the application to return the Autowired clcr object propely.
Edit 1 : spring boot application annotations
#SpringBootApplication
#ComponentScan({"com.cloudtask"})
#EnableAsync
#IntegrationComponentScan({"com.cloudtask"})
#EnableIntegrationManagement(defaultLoggingEnabled = "true")
#EnableJpaRepositories
#EntityScan
public class imclassApplication ```
When you work with Spring Data Jpa with those basic points you should also keep track of below points.
you have added spring-boot-starter-data-jpa in your pom.xml
you have added the entity and repo package below one level of the application package.
If you the package is at same level you should specify the exact package details in the annotation. in your case it should be like :
#EnableJpaRepositories("com.cloudtask.batchconfig.repo")
#EntityScan(basePackages = {"com.cloudtask.batchconfig.entity"})
Happy programming!

SpringBoot projects properties not getting picked up by beans in XML Config

I have a SpringBoot Project and everything works fine locally. Now i have created a runnable jar and trying to execute it.
Project is failing to startup as the beans defined in Spring XML config are not picking up properties defined in property files.
These xml configs are not my own but imported from team libraries so i cannot edit them.
Can someone please advise on what can be done to make it work.
Following are the annotations used in Application.java file. I am not able to add #EnableAutoConfiguration as its implicit and i canot add it here.
#SpringBootApplication(exclude = {
JmxAutoConfiguration.class,
JmsAutoConfiguration.class,
ActiveMQAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
#ComponentScan(excludeFilters={
#ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE, value=RequestReducingAspect.class)})
#EnableScheduling
#EnableSwagger2
#EnableTransactionManagement(mode= AdviceMode.ASPECTJ)
#EnableLoadTimeWeaving(aspectjWeaving= EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
#EnableAspectJAutoProxy
Edit:
#Configuration
#EnableAutoConfiguration
#ImportResource({"classpath:spring-valuation-operations.xml","classpath:spring-trade-info.xml","classpath:spring-ib-valuation-api-service.xml","classpath:caching.xml","classpath:applicationContext-valuations-addin.xml","classpath:regtest-common.xml"})
public class TradeSprayerContext {
}

Spring Boot multiproject Spring Data Jpa

I have a three module Spring Boot App. I've a problem when I try to add a Custom repositoryFactoryBeanClass and when i do that i get the following error:
No constructor with 0 arguments defined in class
'org.springframework.data.jpa.datatables.repository.DataTablesRepositoryFactoryBean'
My annotation is:
#EnableJpaRepositories(repositoryFactoryBeanClass =
DataTablesRepositoryFactoryBean.class, basePackages =
"xxx.xxxxxxx.xxxx.xxxxx.repositories")
I have three modules: web, entity/repository and service.
Thanks.
I was able to solve this by upgrading Spring Boot version to 1.5.4.RELEASE. Please refer this thread for more details https://github.com/darrachequesne/spring-data-jpa-datatables/issues/50
Finally,I changed #EnableJpaRepositories to
#EnableJpaRepositories(basePackages = "xxxx.xxxx.xxxxx.repositories", repositoryBaseClass = DataTablesRepositoryImpl.class)

Spring Boot #SpringBootApplication Annotation is not working for classes in different packages

I am developing a sample SpringBoot Application. I have two packages
1. com.A( in which main class annotated with #springbootApplication is there)
2. com.B(other spring beans).
Now My query is : Spring beans which are in package B are not getting scanned because of that application failing.I tried using
a. #springbootapplicatio(scanBasePackages="com.B")
b. Also #componentScan(..)
c. Used #EnableConfiguration also.
However If I move B package beans under A package,then everything works fine(because #springbootapplication takes care of that).
Please help me to resolve this!
Rahul Kumar
Your configuration class containing the #SpringbootApplication annotation will scan all the classes in the same package in which it is present . You can mention the base package name with the annotation and try it ..
Something like below:
#SpringBootApplication(scanBasePackages = {"com.basepackage"})
This should work:
#SpringBootApplication(scanBasePackages = {"com.A", com.B"})

How to Autowire repository interface from a different package using Spring Boot?

I am new to Spring Boot and want to autowire a repository from a different package in a Rest Controller. It seems that when I place the interface and implementation in a different package the actual controller the autowire seems to fail.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.acme.repository.RawDataRepository] found for dependency:..
Controller:
package com.acme.controller;
import com.acme.repository.RawDataRepository;
// imports removed!
#RestController
#EnableAutoConfiguration
#ComponentScan("com.acme")
public class DataCollectionController {
#Autowired
private RawDataRepository repository;
// code removed!
}
I have tried to use the #ComponentScan annotation but this gives no solution.
Any idea what i am missing? Whenever i put the interface into the package in which the controller resides then all goes well.
If you have Spring Data #Repositories in a different package you have to explicitly #EnableJpaRepositories (or replace "Jpa" with your own flavour). Boot takes it's defaults from the package containing the #EnableAutoConfiguration so it might work to just move that class as well.
You have to use following two annotations
#EnableJpaRepositories(basePackages = "package-name")
#EntityScan(basePackages = "package-name")
EnableJpaRepositories will enable repository if main class is in some different package.
You need to use EntityScan as well to point to package where you have your entity beans or else it will fail with 'Bean is not of managed type' error.
Spring Boot Provides annotations for enabling Repositories.
So whenever someone uses any Repository (it can be JPARepository , CassandraReposotory) it should be enabled in Application Class itself.
Example:
#EnableCassandraRepositories("package name")
#EnableJpaRepositories("package name")
After providing the above annotations, the container takes care of injecting beans for repositories as well.

Resources