Spring Boot Sping Boot 为JpaRepository创建bean时出错

bf1o4zei  于 2023-04-11  发布在  Spring
关注(0)|答案(5)|浏览(296)

我遇到了一个问题,我的学校项目,我试图建立h2数据库。一切都运行良好,我猜,直到我创建了我的数据库存储库接口,它看起来很简单,就像这样

package com.protonmail.jan.backend.repository;

import org.springframework.data.jpa.repository.JpaRepository;

public interface RecordRepository extends JpaRepository {
}

当我在没有存储库代码的情况下运行应用程序时,应用程序至少启动了,我对spring-boot很陌生,所以我不知道这里发生了什么,但我猜这是一些依赖项或需要创建我的pom.xml文件中缺少的beans?请帮助,这里是我得到的一些错误日志:

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.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Entity
public class Room extends AbstractEntity{
    private String name = "pokoj";
    private Boolean lit = false;
    private long temp = 0;

    public Room() {
    }
    public Boolean getLit() {
        return lit;
    }
    public void setLit(Boolean lit) {
        this.lit = lit;
    }
    public long getTemp() {
        return temp;
    }
    public void setTemp(long temp) {
        this.temp = temp;
    }
}

这是AbstractEntity类

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> {}
vfhzx4xs

vfhzx4xs1#

请在接口声明中指定托管对象和id类型:

public interface RecordRepository extends JpaRepository<PersitentClass, ClassId> {
}

其中:PersistentClass-实体模型的类型ClassId -'id'字段的类型
有关类的详细信息,请参阅JpaRepository doc

roejwanj

roejwanj3#

我一直无法帮助spring为扩展JpaRepository的自定义接口创建bean。尝试了所有方法,最终答案是Springboot希望以某种方式命名包,以便所有组件都可以自动连接。
1.遵循Sping Boot JPA CRUD的任何常见示例,如1
1.通常,代码的包部分会被裁剪。这就是诀窍所在。Sping Boot 期望Application类,即标记为@SpringbootApplication的类位于包com.base.app中,而repository接口位于com. base. app. repo中。如果您使用com.base.repo,则无法定位和创建bean!!

q3qa4bjr

q3qa4bjr4#

我已经看过你的代码了。如果你使用的是Sping Boot 版本4或更高版本的javax.persistence.;是depricated.而不是这个导入“importjakarta.persistence
如果您使用的Sping Boot 版本小于3,请检查包结构。您的SpringbootSpringApplication.run()应该在“package com.protonmail.jan.backend”下。
希望这能解决你的问题。
如果你想知道如何使用包层次结构,你可以访问这个链接:https://www.youtube.com/watch?v=ug53epaPFrA&ab_channel=CodeJava
请注意:我不是这个链接视频的所有者。我发现很有帮助,所以分享了它。

ruyhziif

ruyhziif5#

你可以通过在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);
    }
}

相关问题