Spring-data-jpa,Hibernate错误的SQL查询返回null而不是Exception

时间:2012-03-28 13:57:21

标签: java spring spring-data-jpa

我有一个Spring 3.1项目,使用spring-data-jpa进行持久化,Hibernate作为持久性引擎,Postgresql 9.1作为数据库系统。
我创建了一个带有jpa注释的域对象(Passport)和一个相应的repository,扩展了JpaRepository以获得基本的CRUD功能。
出于测试目的,我创建了一个简单的JUnit测试类(见下文)来测试从数据库加载Passport对象。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/applicationContext.xml")
@TransactionConfiguration
@Transactional
public class PassportRepositoryTest {

    @Resource
    protected PassportRepository repository;

    @Test
    public void testFindById() {
        Passport passport = repository.findOne(1L);
        assertNotNull("did not find expected entity", passport);
    }
}

当我运行测试时失败,因为null返回findOne(1L),尽管数据库中有一个带有该主要ID的护照对象。 我检查了输出日志:

15:38:56.101 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@11cf3710] for JPA transaction
15:38:56.101 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Participating in existing transaction
15:38:56.170 [main] DEBUG org.hibernate.loader.Loader - Loading entity: [com.gmi.lab.browser.server.domain.germplasm.Passport#1]
15:38:56.319 [main] DEBUG org.hibernate.SQL - select passport0_.div_passport_id as div1_2_3_, passport0_.accename as accename2_3_, passport0_.accenumb as accenumb2_3_, passport0_.div_accession_collecting_id as div7_2_3_, passport0_.comments as comments2_3_, passport0_.sampstat as sampstat2_3_, passport0_.source as source2_3_, passport0_.div_taxonomy_id as div8_2_3_, accessionc1_.div_accession_collecting_id as div1_3_0_, accessionc1_.collCode as collCode3_0_, accessionc1_.collDate as collDate3_0_, accessionc1_.collNumb as collNumb3_0_, accessionc1_.collSrc as collSrc3_0_, accessionc1_.collector as collector3_0_, accessionc1_.div_locality_id as div7_3_0_, locality2_.id as id0_1_, locality2_.city as city0_1_, locality2_.country as country0_1_, locality2_.elevation as elevation0_1_, locality2_.latitude as latitude0_1_, locality2_.loAccession as loAccess6_0_1_, locality2_.localityName as locality7_0_1_, locality2_.longitude as longitude0_1_, locality2_.origcty as origcty0_1_, locality2_.stateProvince as statePr10_0_1_, taxonomy3_.div_taxonomy_id as div1_1_2_, taxonomy3_.commonName as commonName1_2_, taxonomy3_.genus as genus1_2_, taxonomy3_.population as population1_2_, taxonomy3_.race as race1_2_, taxonomy3_.species as species1_2_, taxonomy3_.subspecies as subspecies1_2_, taxonomy3_.subtaxa as subtaxa1_2_, taxonomy3_.termAccession as termAcce9_1_2_ from Passport passport0_ left outer join AccessionCollection accessionc1_ on passport0_.div_accession_collecting_id=accessionc1_.div_accession_collecting_id left outer join Locality locality2_ on accessionc1_.div_locality_id=locality2_.id left outer join Taxonomy taxonomy3_ on passport0_.div_taxonomy_id=taxonomy3_.div_taxonomy_id where passport0_.div_passport_id=?
15:38:56.769 [main] DEBUG o.h.e.i.StatefulPersistenceContext - Initializing non-lazy collections
15:38:56.770 [main] DEBUG org.hibernate.loader.Loader - Done entity load

当我测试用于检索护照的SQL查询时,我意识到数据库中的表名与类名不匹配(基本上我忘了在@Table中指定Passport注释类)。

问题: 为什么Hibernate / spring-data-jpa在SQL查询失败而不是返回null时抛出异常?

更新: 我忘了在数据库连接中指定数据库。一旦我修复了这个,当SQL语句错误时我也得到了异常。

0 个答案:

没有答案