我从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$1
package 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
@Data
public 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
@Slf4j
public 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()));
};
}
*/
相关分类