我正在尝试为所有存储库创建自定义方法,并得到此错误。谢谢你的帮助!DailyData:
package com.test.demo.Entities;
import javax.persistence.*;
@Entity
@Table(name = "Daily_Data")
public class DailyData {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name = "Day")
private int time;
@Column(name = "Average_Energy_Value")
private double dailyAvgEnergy_value;
@Column(name = "Maximum_Energy_Value")
private double dailyMaxEnergy_value;
@Column(name = "Minimum_Energy_Value")
private double dailyIdleEnergy_value;
@Column(name = "topics")
private String topics;
}
HourlyData:
package com.test.demo.Entities;
import javax.persistence.*;
@Entity
@Table(name = "HourlyData")
public class HourlyData {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name = "Hour")
private int time;
@Column(name = "Average_Energy_Value")
private double hourlyAvgEnergy_value;
@Column(name = "Maximum_Energy_Value")
private double hourlyMaxEnergy_value;
@Column(name = "Idle_Energy_Value")
private double hourlyIdleEnergy_value;
@Column(name = "topics")
private String topics;
}
每周数据:
package com.test.demo.Entities;
import javax.persistence.*;
import java.sql.Timestamp;
@Entity
@Table(name = "Weekly_Data")
public class WeeklyData {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name = "Week")
private int time;
@Column(name = "Average_Energy_Value")
private double weeklyAvgEnergy_value;
@Column(name = "Maximum")
private double weeklyMaxEnergy_value;
@Column(name = "Weekly_Data")
private double weeklyIdleEnergy_value;
@Column(name = "Weekly_Data")
private String topics;
}
RawData:
package com.test.demo.Entities;
import javax.persistence.*;
@Entity
@Table(name="Raw_Data")
public class RawData {
//same as in MqttDataModel
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
public Long id;
//TODO:mqttID?
//Same
@Column(name = "timestamp")
public double timestamp;
//Same
@Column(name = "energy_value")
public double energy_value;
//Same
@Column(name="topics")
public String topics;
public String getTopics() {
return topics;
}
public void setTopics(String topics) {
this.topics = topics;
}
public double getEnergy_value() {
return energy_value;
}
public void setEnergy_value(double energy_value) {
this.energy_value = energy_value;
}
public double getTimestamp() {
return timestamp;
}
public void setTimestamp(double timestamp) {
this.timestamp = timestamp;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
通用存储库:
package com.test.demo.GenericRepo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.io.Serializable;
import java.util.List;
@NoRepositoryBean
public interface GenericRepository<T,Long extends Serializable> extends JpaRepository<T, Long> {
public List<T> findFromToTimestamp(T t, double beginTime, double endTime);
public List<T> findByTime(T t, double time);
public List<T> findByTopics(T t, List<String> topics);
}
GenericRepositoryImpl:
package com.test.demo.GenericRepo;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
public class GenericRepositoryImpl<T, Long extends Serializable>
extends SimpleJpaRepository<T, Long> implements GenericRepository<T, Long> {
private EntityManager entityManager;
public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
}
@SuppressWarnings("unchecked")
@Transactional
@Override
public List<T> findFromToTimestamp(T t, double beginTime, double endTime) {
return entityManager.createQuery("SELECT * FROM :t WHERE timestamp BETWEEN :begin AND :end").setParameter("t", t.getClass()).setParameter("begin", beginTime).setParameter("end", endTime).getResultList();
}
@SuppressWarnings("unchecked")
@Transactional
@Override
public List<T> findByTime(T t, double time) {
return entityManager.createQuery("SELECT * FROM :t WHERE timestamp like :v").setParameter("t", t.getClass()).setParameter("v", time).getResultList();
}
@SuppressWarnings("unchecked")
@Transactional
@Override
public List<T> findByTopics(T t, List<String> topics) {
return entityManager.createQuery("SELECT * FROM :t WHERE topics like :v").setParameter("t", t.getClass()).setParameter("v", topics.toString()).getResultList();
}
}
RawDataRepository:
package com.test.demo.Repositories;
import com.test.demo.Entities.RawData;
import com.test.demo.GenericRepo.GenericRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RawDataRepository extends GenericRepository<RawData, Long> {
}
其他存储库和服务看起来是一样的DailyDataService:
package com.test.demo.Services;
import com.test.demo.Repositories.DailyDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DailyDataService {
DailyDataRepository dailyDataRepository;
}
应用程序:
package com.test.demo;
import com.test.demo.GenericRepo.GenericRepositoryImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.test.demo.Repositories",
repositoryBaseClass = GenericRepositoryImpl.class)
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testdb2</name>
<description>demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
错误log:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:874) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1344) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:693) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:510) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:374) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:134) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:936) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.23.jar:5.3.23] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.23.jar:5.3.23] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.5.jar:2.7.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.5.jar:2.7.5] at com.test.demo.TestApplication.main(TestApplication.java:16) ~[classes/:na]
我把所有的东西都检查了一遍又一遍,但我真的不知道哪里出错了。
我怀疑问题是您混合了不同版本的Spring Boot。在3.0.0和2.7.5版本中,这导致了依赖关系的奇怪混合。您的依赖管理也违背了父组件的目的,父组件为您管理所有版本。您应该从您正在使用的依赖项中删除version
标记。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testdb2</name>
<description>demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
清理后的pom将使用Spring Boot 2.7.5依赖项(在父组件中声明)。如果你想切换到3.0.0,只需更改父版本。
注意:当切换到Spring Boot 3.0.0时,您需要使用JakartaEE类(不同的包)而不是JavaEE包。