我戴了一个基于
java的应用程序并使用了
Spring Boot
这是模型:
@Entity
@Table(name = "task_list")
public class Task implements Serializable
这是Spring启动使用它启动的Config类:
@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();
}
这是应用程序:
@SpringBootApplication()
public class Application {
public static void main(String[] args) {
SpringApplication.run(Config.class);
}
}
因此,当我运行应用程序时,它可以工作并创建所有bean但是
当我想与数据库交互时,Hibernate得到了这个错误
org.hibernate.MappingException: Unknown entity: model.Task
我认为这是因为没有任何persistence.xml要映射
模特班,
那么我应该在Spring启动应用程序中做什么?
应该把那个xml放在哪里?
是否有任何Annotation告诉spring boot来映射模型类?
提前致谢 .
最佳答案 感谢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;
}