JPA не может найти метамодель Entity, даже если она присутствует в коде - PullRequest
0 голосов
/ 04 июня 2018
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.

вот мой класс сущностей

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);
}
}

Вот мой 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>

вот мой интерфейс, который расширяет хранилище

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

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

вот мой класс обслуживания

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);
}

}

Вотмой контроллер

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); 

}

}
...