Where to define spring's Entity model class to load

I wore a Java based application and used Spring Boot

This is the model:

@Entity
@Table(name = "task_list")
public class Task implements Serializable 

And this is the Config class that Spring uses to run it:

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableTransactionManagement
@ComponentScan(basePackages = {"controller", "dao", "service"})
class Config {


    @Bean(name = "dataSource")
    public DataSource dataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL).build();
    }

    @Bean(name = "entityManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.HSQL);
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(getClass().getPackage().getName());
        factory.setDataSource(dataSource());

        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

And this application:

@SpringBootApplication()
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Config.class);

    }
}

So, when I run the application, it works and creates an all bean BUT when I want to interact with the database, Hibernate got this error

org.hibernate.MappingException: Unknown entity: model.Task

I think this is because there is no persistence.xml to map the model class to,

What should I do in a Spring boot application? where to put this xml? Are there any annotations that Spring boot reports for mapping model classes?

Thanks in advance.

+4
source share
1 answer

Xtreme Biker

LocalContainerEntityManagerFactoryBean.

@Bean(name = "entityManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.HSQL);
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new     LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("model");
        factory.setDataSource(dataSource());

        return factory;
    }
+2

All Articles