Reputation: 2120
Im creating a spring boot app. I want to save my testEntity in database. Im following this tutorial: https://spring.io/guides/gs/accessing-data-jpa/ A table should be created automaticly to save Entity.
However when i try to run it as Spring Booot App im getting follwoing errors:
Error creating bean with name 'demo' defined in backend.Application: Unsatisfied dependency expressed through constructor argument with index 0 of type [test.EntityRepo]: : No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Please explain what am i doing wrong and how to fix?
Below you can find datasource configuration and classes.
application.properties:
spring.datasource.url=jdbc:oracle:thin://localhost:1521/orcl
spring.datasource.username=HR
spring.datasource.password=orcl
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
testEntity:
package test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.springframework.data.annotation.Id;
@Entity
public class testEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long ID;
private String name;
public testEntity() {}
public testEntity(long iD) {
ID = iD;
}
public testEntity(String name) {
this.name = name;
}
public long getID() {
return ID;
}
public void setID(long iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
entityRepository:
package test;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EntityRepo extends CrudRepository<testEntity, Long>{
List<testEntity> findByName(String name);
}
JPA configuration class:
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
class JpaConfiguration {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("test");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
class with main:
@EnableSwagger2
@SpringBootApplication
@EnableMapRepositories
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
Upvotes: 0
Views: 908
Reputation: 2120
Found a solution. JpaConfig class was in default package and no path toit was specified, moving JpaConfig file to package with Aplication fixed the problem
Upvotes: 0
Reputation: 4297
Try This :
@Autowired
@Qualifier("demo")
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
@Repository("entityRepo")
public interface EntityRepo extends CrudRepository<testEntity, Long>{
List<testEntity> findByName(String name);
}
Upvotes: 1
Reputation: 4356
Try this :
@Autowired
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
Upvotes: 0