我從https://spring.io/guides/gs/accessing-data-jpa/獲得了 Spring Data JPA 入門項目。我可以創(chuàng)建Customer和CustomerRepository類并使Customer實體持久存在。但是,當我添加另一個實體Person和 aPersonRepository并嘗試保留該實體時,出現(xiàn)如下所示的錯誤。就像在內部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/150
提交
取消