0
2018-06-04 16:55:38.821  WARN 10092 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topicController': Unsatisfied dependency expressed through field 'topicService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topicService': Unsatisfied dependency expressed through field 'topicRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topicRepository': Cannot create inner bean '(inner bean)#77e80a5e' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#77e80a5e': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2018-06-04 16:55:38.840  INFO 10092 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-06-04 16:55:38.884  INFO 10092 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-04 16:55:39.082 ERROR 10092 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field topicRepository in com.example.demo.topic.TopicService required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

here is my Entity Class

package com.example.demo.topic;
   import javax.persistence.Column;
   import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
   import javax.persistence.Table;
   @Entity
  @Table(name="Topic")
  public class TopicPOJO {
@Id      
 @Column(name = "Id", nullable = false)
String id;
 @Column(name = "name", nullable = true)
String name;
 @Column(name = "desc", nullable = true)
String description;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public TopicPOJO(String id, String name, String description) {
    super();
    this.id = id;
    this.name = name;
    this.description = description;
}

public TopicPOJO() {
    super();

}

}


**here is my application**
package com.example.demo.topic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoJpaApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoJpaApplication.class, args);
}
}

Here is My POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo-JPA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo-JPA</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

here is my interface which extends repository

package com.example.demo.topic;
import org.springframework.data.repository.CrudRepository;

public interface TopicRepository extends CrudRepository<TopicPOJO,String>
{
}

here is my service class

package com.example.demo.topic;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

@Service
public class TopicService {
@Autowired
TopicRepository topicRepository;    
public List<TopicPOJO> getAllTopics()
{   
    List<TopicPOJO> topics=new ArrayList<>();   
    topicRepository.findAll()
    .forEach(topics::add);  
    return topics;
}

public TopicPOJO getTopic(String id)
{
    return topicRepository.findById(id).get();  
}

public void addTopic(TopicPOJO topic)
{ 
    topicRepository.save(topic);

}

public void updateTopic(String id,TopicPOJO topic) {


    topicRepository.save(topic);
}


public void deleteTopic(String id) {

    topicRepository.deleteById(id);
}

}

Here is my Controller

package com.example.demo.topic;
import java.util.List;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class TopicController {

@Autowired
TopicService topicService;

@RequestMapping("/topic")
public List<TopicPOJO> getAlltopics()
{


    return topicService.getAllTopics(); 

}


@RequestMapping("/topic/{id}")
public TopicPOJO gettopic(@PathVariable String id)
{


    return topicService.getTopic(id);

}

@RequestMapping(method=RequestMethod.POST,value="/topic")
public void addTopic(@RequestBody TopicPOJO topic)
{


     topicService.addTopic(topic); 

}

@RequestMapping(method=RequestMethod.PUT,value="/topic/{id}")
public void updateTopic(@RequestBody TopicPOJO topic,@PathVariable String 
id)
{


     topicService.updateTopic(id,topic); 

}

@RequestMapping(method=RequestMethod.DELETE,value="/topic/{id}")
public void deleteTopic(@PathVariable String id)
{


     topicService.deleteTopic(id); 

}

}
Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
  • Did You configured database config with @EnableJpaRepositories – Igor Vuković Jun 04 '18 at 08:21
  • tried ....doesnt work – Hrushikesh Godse Jun 04 '18 at 08:48
  • can you provide your `application.properties`? – Anh N. Nguyen Jun 04 '18 at 09:43
  • i havent configured anything in dat as i m using embedded apache derby DB should i configure something in application.properities ?? – Hrushikesh Godse Jun 04 '18 at 09:54
  • right now i have just set the port – Hrushikesh Godse Jun 04 '18 at 09:55
  • server.port=3200 – Hrushikesh Godse Jun 04 '18 at 09:55
  • A "JPA metamodel" presumably refers to the JPA static metamodel, that are generated for use by JPA Criteria (the _ classes). Have you generated them?!! –  Jun 04 '18 at 09:56
  • @BillyFrost can u explain what are u saying plz? actually i m new in spring boot – Hrushikesh Godse Jun 04 '18 at 09:57
  • The JPA spec defines a "static metamodel", and it involves the user GENERATING some classes for use at runtime. As per http://www.datanucleus.org:15080/products/accessplatform_5_1/jpa/query.html#metamodel This is the JPA API. A standard. As opposed to Spring Data JPA which simply uses what JPA provides. Who knows if this Spring message refers to that, but then I'd expect any Spring documentation to tell you that –  Jun 04 '18 at 10:07
  • Add your application class as well as the full stacktrace. – M. Deinum Jun 04 '18 at 11:01
  • @M.Deinum `package com.example.demoJPA; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoJpaApplication { public static void main(String[] args) { SpringApplication.run(DemoJpaApplication.class, args); } } ` – Hrushikesh Godse Jun 04 '18 at 11:18
  • You aren't following the advice of the Spring Team. Put it in a top level package for your project like `com.example.demo`. Spring Boot will scan all the packages and sub packages it finds starting from the class the `@SpringBootApplication` annotation is on. All your other classes are in `com.example.demo.topic` and thus not covered. Either move your class or manually configure all the packages for component-scan, entity-scan and enable jpa repositories. – M. Deinum Jun 04 '18 at 11:20
  • @M.Deinum Thank you dat exception has gone but now i m facing exeption-: tion - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topicController': Unsatisfied dependency expressed through field 'topicService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topicService': Unsatisfied dependency expressed through field 'topicRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: – Hrushikesh Godse Jun 04 '18 at 11:29
  • @M.Deinum i have edited question – Hrushikesh Godse Jun 04 '18 at 11:33
  • That basically is a different question... And without the full stracktrace it is hard to pinpoint. – M. Deinum Jun 04 '18 at 11:35
  • @M.Deinum i have edited full stack – Hrushikesh Godse Jun 04 '18 at 11:43
  • Please open a new question instead of editing this one into a new question. Your initial question (the metamodel one has been answered so please re-instate that question). – M. Deinum Jun 04 '18 at 12:02
  • @M.Deinum MY problem is solved i missed annoation "@repository" on TopicRepository – Hrushikesh Godse Jun 04 '18 at 13:11
  • That shouldn't be needed assuming you use Spring Data JPA as that should detect it automatically. – M. Deinum Jun 04 '18 at 13:24

0 Answers0