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

我从https://spring.io/guides/gs/accessing-data-jpa/获得了 Spring Data JPA 入门项目。我可以创建CustomerCustomerRepository类并使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()));

        };

    }

    */




LEATH
浏览 91回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java