为了账号安全,请及时绑定邮箱和手机立即绑定

无法将第二个实体添加到 Spring 数据启动项目

无法将第二个实体添加到 Spring 数据启动项目

LEATH 2023-04-19 16:17:33
我从https://spring.io/guides/gs/accessing-data-jpa/获得了 Spring Data JPA 入门项目。我可以创建Customer和CustomerRepository类并使Customer实体持久存在。但是,当我添加另一个实体Person和 aPersonRepository并尝试保留该实体时,出现如下所示的错误。就像在内部Application.java有某种内部类$1,Spring 数据试图将其映射为一个实体。java.lang.IllegalArgumentException: 未知实体: hello.Application$1package hello;import lombok.Data;import lombok.NoArgsConstructor;import lombok.ToString;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entity@ToString@NoArgsConstructor@Datapublic class Person {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Long id;    private int age;}package hello;import org.springframework.data.repository.CrudRepository;public interface PersonRepository extends CrudRepository<Person, Long> {}package hello;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplication@Slf4jpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class);    }    /*    @Bean    public CommandLineRunner demo(CustomerRepository repository) {        return (args) -> {            repository.save(new Customer("Jack", "Bauer"));            repository.save(new Customer("Chole", "O'Brian"));            repository.save(new Customer("Kim", "Bauer"));            repository.save(new Customer("David", "Palmer"));            repository.save(new Customer("Michelle", "Dessler"));            // fetch all customers            log.info("Customers found with findAll():");            log.info("-------------------------------");            repository.findAll().forEach(c -> log.info(c.toString()));        };    }    */
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信