Java Spring boot在为JpaRepository创建bean时出错
我在尝试为我的学校项目建立h2数据库时遇到了一个问题。 我想在我为我的数据库创建存储库接口之前,一切都运行得很好Java Spring boot在为JpaRepository创建bean时出错,java,spring-boot,h2,Java,Spring Boot,H2,我在尝试为我的学校项目建立h2数据库时遇到了一个问题。 我想在我为我的数据库创建存储库接口之前,一切都运行得很好 package com.protonmail.jan.backend.repository; import org.springframework.data.jpa.repository.JpaRepository; public interface RecordRepository extends JpaRepository { } 当我在没有存储库代码的情况下运行应用程序时
package com.protonmail.jan.backend.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface RecordRepository extends JpaRepository {
}
当我在没有存储库代码的情况下运行应用程序时,应用程序至少会启动,我对spring boot还很陌生,所以我不知道这里发生了什么,但我猜我的pom.xml文件中缺少了一些依赖项或需要创建bean的东西?请帮忙,这是我得到的一些错误日志:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
这是我的记录
package com.protonmail.jan.backend.entity;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Record extends AbstractEntity {
@NotNull
@NotEmpty
private Date date;
@NotNull
@NotEmpty
private static Map<Integer, Room> rooms;
static {
rooms = new HashMap<Integer, Room>(){
{
put(1, new Room());
put(2, new Room());
put(3, new Room());
}
};
}
public Collection<Room> getAllRooms(){
return this.rooms.values();
}
}
这是抽象实体类
package com.protonmail.jan.backend.entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class AbstractEntity {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Long id;
public Long getId() {
return id;
}
public boolean isPersisted() {
return id != null;
}
@Override
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AbstractEntity other = (AbstractEntity) obj;
if (getId() == null || other.getId() == null) {
return false;
}
return getId().equals(other.getId());
}
}
我也用过
public interface RecordRepository extends JpaRepository<Repository, Long> {}
public interface RecordRepository扩展了JpaRepository{}
请在接口声明中指定托管对象和id类型:
public interface RecordRepository extends JpaRepository<PersitentClass, ClassId> {
}
public interface RecordRepository扩展了JpaRepository{
}
其中:
PersientClass—实体模型的类型
ClassId—“id”字段的类型
有关类的详细信息,请参阅。请在接口声明中指定托管对象和id类型:
public interface RecordRepository extends JpaRepository<PersitentClass, ClassId> {
}
public interface RecordRepository扩展了JpaRepository{
}
其中:
PersientClass—实体模型的类型
ClassId—“id”字段的类型
有关课程详细信息,请参阅。
@EnableJpaRepositories
配置类上的注释
同样如此,
@Repository
@EnableJpaRepositories
配置类上的注释
同样,您可以通过将以下内容添加到spring boot开始运行的主类来解决此问题:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan("com.mine.alien") // path of the entity model
@EnableJpaRepositories("com.mine.demo.control") // path of jpa repository
@SpringBootApplication
public class SpringJpah2Application {
public static void main(String[] args) {
SpringApplication.run(SpringJpah2Application.class, args);
}
}
您可以通过在spring boot开始运行的主类中添加以下内容来解决此问题:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan("com.mine.alien") // path of the entity model
@EnableJpaRepositories("com.mine.demo.control") // path of jpa repository
@SpringBootApplication
public class SpringJpah2Application {
public static void main(String[] args) {
SpringApplication.run(SpringJpah2Application.class, args);
}
}
您是否已自动连接RecordRepository?您将收到有关原始类型的警告。注意它们。具体来说,您应该使用
@autowired annotation参数化JpaRepository
。@autowired annotation在RecordRepository中不起作用,它表示autowired annotation不能应用于类型。您是否有autowired RecordRepository?您将收到有关原始类型的警告。注意它们。具体来说,您应该使用
参数化JpaRepository
。@autowired annotation在RecordRepository中不起作用,它说autowired annotation不能适用于类型我已经尝试过了,但问题仍然是相同的。您在这之后执行mvn清理验证吗?@DanielJacob我也尝试过,没有结果,同样的问题,最后是类路径上的依赖关系?我已经试过了,但问题仍然是你在那之后做mvn清理验证的相同ID?@DanielJacob我也试过了,没有结果,同样的问题,最后是对你的类路径的依赖?我是否只需在Record类上键入@EnableJpaRepositories
就可以做到这一点?我不知道你所说的配置类是什么意思。我在Record类和RecordRepository上都添加了此注释,但如果您使用spring boot,则此注释似乎不起作用。将其添加到应用程序Classis上并没有完全解决此问题。但它修复了日志中的一些问题。创建名为“recordRepository”的bean时,会出现以下错误:java.lang.IllegalArgumentException:不是托管类型:interface org.springframework.data.repository.repository我是否只需在Record类上键入@EnableJpaRepositories
?我不知道你所说的配置类是什么意思。我在Record类和RecordRepository上都添加了此注释,但如果您使用spring boot,则此注释似乎不起作用。将其添加到应用程序Classis上并没有完全解决此问题。但它修复了日志中的一些问题。创建名为“recordRepository”的bean时出现错误,原因是:java.lang.IllegalArgumentException:不是托管类型:interface org.springframework.data.repository.repository