我得到了FooBar is not mapped例外,即使我已经放置了所有必要的注释:
模型类中的实体注解:
模型/FooBar.java*
@Data
@Entity
@Table(name = "ORDERS")
public class FooBar {}
DAO 中的 Repository、PersistenceContext 和 Transactioinal。另外,我正确地用类的名称“FooBar”引用了它
db/FooBarDao.java
@Repository
public class OrderDAO {
@PersistenceContext
private EntityManager em;
@Transactional
public List<FooBar> getFooBars() {
return em.createQuery(
"select fb from FooBar fb",
FooBar.class
).getResultList();
}
}
这是使其全部正常工作的配置:
conf/DbConfig.java
@EnableTransactionManagement
@Configuration
@ComponentScan(basePackages = {"project.db"})
public class DbConfig {
@Autowired
public Environment env;
@Bean
public DataSource dataSource() {
System.out.println("Configuring database!!!!!!!!");
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:mem:myjdbc");
new JdbcTemplate(ds)
.update(FileUtil.readFile2("./src/main/java/project/db/sql/initialize.sql"));
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
factory.setPackagesToScan("model");
factory.setDataSource(dataSource());
factory.setJpaProperties(additionalProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
}
隔江千里
相关分类