出现错误:在类路径资源中创建名称为'entityManagerFactory'的bean时出错

时间:2018-08-01 13:44:58

标签: spring-mvc spring-data-jpa thymeleaf

我是spring和spring boot应用程序的新手。获取在类路径资源中定义的名称为'entityManagerFactory'的创建bean的错误。它说无法建立EntityManagerFactory。控制台中显示错误:

2018-08-01 17:47:38.920[0;39m [31mERROR[0;39m [35m19412[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.boot.SpringApplication              [0;39m [2m:[0;39m Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.telenor.wipro.logging.jpa.entity.FuseTPS
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at com.telenor.wipro.logging.logserviceapp.LoggerServiceApplication.main(LoggerServiceApplication.java:18) [classes/:na]
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.telenor.wipro.logging.jpa.entity.FuseTPS
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:731) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:249) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1767) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 16 common frames omitted

我的应用程序起点: LoggerServiceApplication.java

package com.telenor.wipro.logging.logserviceapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication(scanBasePackages="com.telenor.wipro.logging.*")
@EnableJpaRepositories(basePackages = "com.telenor.wipro.logging.jpa.repository")
@EntityScan(basePackages = "com.telenor.wipro.logging.jpa.entity")
@EnableScheduling
@EnableWebMvc
public class LoggerServiceApplication {

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

我的控制器类: LoggingServiceWebController.java

package com.telenor.wipro.logging.controller;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.telenor.wipro.logging.dto.Search;
import com.telenor.wipro.logging.dto.SearchTPS;
import com.telenor.wipro.logging.jpa.entity.FuseTPS;
import com.telenor.wipro.logging.jpa.repository.FuseLogsRepository;
import com.telenor.wipro.logging.jpa.repository.FuseTpsRepository;
import com.telenor.wipro.logging.service.LoggerService;

@Controller // This means that this class is a Controller
// This means URL's start with /demo (after Application path)
public class LoggingServiceWebController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingServiceWebController.class);

    @Value("${welcome.message}")
    private String message; 

    @Autowired
    LoggerService loggerService;

    @Autowired
    FuseLogsRepository fuseLogsRepository;

    @Autowired
    FuseTpsRepository fuseTpsRepository;

    @RequestMapping(value="/welcome", method=RequestMethod.GET)
    public String welcome(Map<String, Object> model) {

        LOGGER.debug("Welcome {}", "testing");
        model.put("message", this.message);
        return "welcome";
    }

    @RequestMapping(value="/search", method=RequestMethod.GET)
    public String searchFuseLogs(Model model) {
        model.addAttribute("search", new Search());
        return "search";
    }

    @RequestMapping(value="/search", method=RequestMethod.POST)
    public String getFuseLogs(Model model, @ModelAttribute Search search) {

        String strFromDate = "";
        String strToDate = "";

        if(search.fdate != null && !search.fdate.equalsIgnoreCase("")) {
            strFromDate = search.fdate;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -30);
            strFromDate = new SimpleDateFormat("YYYY-MM-dd").format(cal.getTime());
        }

        strFromDate = strFromDate + " 00:00:00,000";
        System.out.println(strFromDate);
        Date dtFromDate = new Date();
        try {
            dtFromDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS").parse(strFromDate);
        } catch (ParseException e) {
            e.printStackTrace();
        } 
        Timestamp tsFromDate = new Timestamp(dtFromDate.getTime());


        if(search.tdate != null && !search.tdate.equalsIgnoreCase("")) {
            strToDate = search.tdate;
        } else {
            strToDate = new SimpleDateFormat("YYYY-MM-dd").format(Calendar.getInstance().getTime());
        }

        strToDate = strToDate + " 23:59:59,999";
        System.out.println(strToDate);
        Date dtToDate = new Date();
        try {
            dtToDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS").parse(strToDate);
        } catch (ParseException e) {
            e.printStackTrace();
        } 
        Timestamp tsToDate = new Timestamp(dtToDate.getTime());

        System.out.println("From Date: "+tsFromDate);
        System.out.println("To Date: "+tsToDate);

        model.addAttribute("fuseLogs", fuseLogsRepository.findByAll(
                tsFromDate, 
                tsToDate,
                search.tpayload.toUpperCase(),
                search.tguid.toUpperCase(), 
                search.tservicename.toUpperCase(), 
                search.tsourcechannel.toUpperCase(), 
                search.ttargetsystem.toUpperCase()));

        return "show";
    }

    @RequestMapping(value="/SearchTPS", method=RequestMethod.GET)
    public String searchTPS(Model model) {
        model.addAttribute("SearchTPS", new SearchTPS());
        return "SearchTPS";
    }

    @RequestMapping(value="/SearchTPS", method=RequestMethod.POST)
    public String getTPS(Model model, @ModelAttribute SearchTPS searchtps) {
        System.out.println("Service Name"+searchtps.tservicename);

        model.addAttribute("tpslist", fuseTpsRepository.findByTPS());
        return "ShowTPS";
    }

    @GetMapping(path="/all")
    public String getAllLogs(Model model) {
        // This returns a JSON or XML with the users
        model.addAttribute("fuseLogs",fuseLogsRepository.findAll());
        return "all";
    }

}

我的实体类: FuseTPS.java

package com.telenor.wipro.logging.jpa.entity;

import javax.persistence.Table;

@Table(name="fuse_logs")
public class FuseTPS {

    String countTPS;
    String serviceName;

    public String getCountTPS() {
        return countTPS;
    }
    public void setCountTPS(String countTPS) {
        this.countTPS = countTPS;
    }

    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
}

我的实体类: FuseLogs.java

package com.telenor.wipro.logging.jpa.entity;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity // This tells Hibernate to make a table out of this class
@Table(name="fuse_logs")
public class FuseLogs {

    @Column(name = "log_id")
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public long id;

    @Column(name = "log_timestamp")
    Timestamp logTimeStamp;

    @Column(name = "log_level")
    String logLevel;

    @Column(name = "thread_id")
    String threadID;

    @Column(name = "stage")
    String stage;

    @Column(name = "componant_id")
    String componantID;

    @Column(name = "logpoint")
    String logpoint;

    @Column(name = "gu_id")
    String guID;

    @Column(name = "source_cahnnel")
    String sourceCahnnel;

    @Column(name = "target_system")
    String targetSystem;

    @Column(name = "service_name")
    String serviceName;

    @Column(name = "free_text")
    String freeText;

    @Column(name = "container_name")
    String containerName;

    @Column(name = "payload")
    String payload;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Timestamp getLogTimeStamp() {
        return logTimeStamp;
    }

    public void setLogTimeStamp(Timestamp logTimeStamp) {
        this.logTimeStamp = logTimeStamp;
    }

    public String getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public String getThreadID() {
        return threadID;
    }

    public void setThreadID(String threadID) {
        this.threadID = threadID;
    }

    public String getStage() {
        return stage;
    }

    public void setStage(String stage) {
        this.stage = stage;
    }

    public String getComponantID() {
        return componantID;
    }

    public void setComponantID(String componantID) {
        this.componantID = componantID;
    }

    public String getLogpoint() {
        return logpoint;
    }

    public void setLogpoint(String logpoint) {
        this.logpoint = logpoint;
    }

    public String getGuID() {
        return guID;
    }

    public void setGuID(String guID) {
        this.guID = guID;
    }

    public String getSourceCahnnel() {
        return sourceCahnnel;
    }

    public void setSourceCahnnel(String sourceCahnnel) {
        this.sourceCahnnel = sourceCahnnel;
    }

    public String getTargetSystem() {
        return targetSystem;
    }

    public void setTargetSystem(String targetSystem) {
        this.targetSystem = targetSystem;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getFreeText() {
        return freeText;
    }

    public void setFreeText(String freeText) {
        this.freeText = freeText;
    }

    public String getContainerName() {
        return containerName;
    }

    public void setContainerName(String containerName) {
        this.containerName = containerName;
    }

    public String getPayload() {
        return payload;
    }

    public void setPayload(String payload) {
        this.payload = payload;
    }

}

我的存储库界面: FuseTpsRepository.java 我想按表fuse_logs的查询中所述按服务名称访问总计数组,该表已经由spring在FuseLogs.java上面创建了

package com.telenor.wipro.logging.jpa.repository;

import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.telenor.wipro.logging.jpa.entity.FuseTPS;

@Repository
public interface FuseTpsRepository {

    @Query(value="SELECT COUNT(distinct gu_id), service_name FROM fuse_logs WHERE GROUP BY service_name",nativeQuery=true)
    public List<FuseTPS> findByTPS();
}

application.yml

welcome:
  message: Hello 

#For local System
logger-filepath: E:\\JBossEAP\\fuse-logs\\fuse.log


server:
  port: 9092
  context-path: /logger-service

spring:
  datasource:
#    For local System
    url: jdbc:postgresql://127.0.0.1:5432/postgres?currentSchema=public
    driver-class-name: org.postgresql.Driver
    username: postgres
    password: DB@123

  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    show-sql: true 
    hibernate.ddl-auto: update  

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.telenor.wipro.logging</groupId>
    <artifactId>LoggingService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>LoggingService</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.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>
        <!-- Postgress Java Connector library -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1206-jdbc42</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <!-- Optional, for bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

我的模板正文: ShowTPS.html

<body>
<div align="center">
    <h1>About 77,200 results (0.64 seconds)</h1>
    <table>
        <tr>
            <th>Serial No.</th>
            <th>TPS</th>
            <th>Service Name</th>
        </tr>
        <tr th:each="list,iterationStatus  : ${tpslist}">
            <td th:text="${iterationStatus.count}"></td>
            <td th:text="${list.countTPS}"></td>
            <td th:text="${list.serviceName}"></td>
        </tr>
    </table>

</div>
    <!-- <a href="/search">Search Again</a> -->
</body>

我想从fuse_logs表中按service_name访问总数组并将其显示给ShowTPS.html模板

1 个答案:

答案 0 :(得分:0)

错误是 未为实体指定标识符:com.telenor.wipro.logging.jpa.entity.FuseTPS

此类确实令人困惑。它没有@Entity批注,没有@Id,并且引用与FuseLogs相同的表。您需要修复此类。添加所有正确的注释并更正表格。