Spring JPA + Hibernate无法使用JPARepository的findAll()获取记录

时间:2016-03-16 06:01:39

标签: spring hibernate spring-data spring-data-jpa jpql

我使用以下代码创建了配置:

@Configuration
@EnableTransactionManagement
@ComponentScan("Name of package")
@EnableJpaRepositories("Name of package")

public class Config  {
    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "Name of package";

    @Bean
    public DataSource dataSource() {
         DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName("Driver Name");          
         dataSource.setUrl("Url");
         dataSource.setUsername("UserName");
         dataSource.setPassword("Password");
         return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN);
        entityManagerFactoryBean.setJpaProperties(hibProperties());
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        return entityManagerFactoryBean;
    }

    private Properties hibProperties() {
        Properties properties = new Properties();
                properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, "org.hibernate.dialect.DB2Dialect");
                properties.put("hibernate.default_schema","Schema Name");
                return properties;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
          return transactionManager;
       }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
          return new PersistenceExceptionTranslationPostProcessor();
       }
}

有一个实现JPARepository的自定义存储库接口。 我已经在控制器中自动安装了自定义存储库并试图调用findAll()。但是该方法返回0,尽管DB中有3条记录。 我正在使用spring web进行休息服务。

实体类是使用@Entity和@Table注释创建的。它有一个嵌入式键,使用@EmbeddedId注释进行注释。

@Repository 
public interface EntityRepository extends JpaRepository<EntityTable, Long> { 
    @SuppressWarnings("unchecked") EntityTable save(EntityTable entityTable); 
} 

实体表是我用db表映射的表的名称。

0 个答案:

没有答案