标签: spring-data

如何测试Spring Data存储库?

我想要一个UserRepository在Spring Data的帮助下创建的存储库(比方说).我是spring-data(但不是spring)的新手,我使用的是本教程.我选择处理数据库的技术是JPA 2.1和Hibernate.问题是我对如何为这样的存储库编写单元测试一无所知.

我们以create()方法为例.当我正在测试时,我应该为它编写单元测试 - 这就是我遇到的三个问题:

  • 首先,如何将一个模拟注入EntityManager一个UserRepository接口的不存在的实现?Spring Data将基于此接口生成实现:

    public interface UserRepository extends CrudRepository<User, Long> {}
    
    Run Code Online (Sandbox Code Playgroud)

    但是,我不知道如何强制它使用EntityManager模拟和其他模拟 - 如果我自己编写了实现,我可能会有一个setter方法EntityManager,允许我使用我的模拟进行单元测试.(至于实际的数据库连接,我有一个JpaConfiguration类,有注释@Configuration@EnableJpaRepositories,通过编程定义豆类DataSource,EntityManagerFactory,EntityManager等等-但库应该是测试友好,并允许重写这些事情).

  • 其次,我应该测试互动吗?这是我很难找出什么方法EntityManagerQuery应该被称为(类似于那个verify(entityManager).createNamedQuery(anyString()).getResultList();),因为它是不是我是谁写的实施.

  • 第三,我是否应该首先对Spring-Data生成的方法进行单元测试?据我所知,第三方库代码不应该进行单元测试 - 只有开发人员自己编写的代码应该进行单元测试.但如果这是真的,它仍然会将第一个问题带回现场:比方说,我的存储库有几个自定义方法,我将编写实现,如何注入我的模拟EntityManagerQuery进入最终,生成库?

注:我将使用测试驱动我的仓库集成和单元测试.对于我的集成测试,我使用的是HSQL内存数据库,显然我没有使用数据库进行单元测试.

也许第四个问题,在集成测试中测试正确的对象图创建和对象图检索是否正确(比方说,我有一个用Hibernate定义的复杂对象图)?

更新:今天我继续尝试模拟注入 - 我创建了一个静态内部类来允许模拟注入.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UserRepositoryTest {

@Configuration
@EnableJpaRepositories(basePackages = …
Run Code Online (Sandbox Code Playgroud)

unit-testing jpa spring-data spring-data-jpa

119
推荐指数
8
解决办法
13万
查看次数

Spring-Data-JPA注释的setMaxResults?

我正在尝试将Spring-Data-JPA合并到我的项目中.让我困惑的一件事是如何通过注释实现setMaxResults(n)?

例如,我的代码:

public interface UserRepository extends CrudRepository<User , Long>
{
  @Query(value="From User u where u.otherObj = ?1 ")
  public User findByOhterObj(OtherObj otherObj);
}
Run Code Online (Sandbox Code Playgroud)

我只需one (and only one)要从otherObj 返回用户,但我找不到一种方法来注释maxResults ...有人可以给我一个提示吗?

(mysql抱怨:

com.mysql.jdbc.JDBC4PreparedStatement@5add5415: select user0_.id as id100_, user0_.created as created100_ from User user0_ where user0_.id=2 limit ** NOT SPECIFIED **
WARN  util.JDBCExceptionReporter - SQL Error: 0, SQLState: 07001
ERROR util.JDBCExceptionReporter - No value specified for parameter 2
Run Code Online (Sandbox Code Playgroud)

)

我找到了一个链接:https://jira.springsource.org/browse/DATAJPA-147,我试过但失败了.现在似乎不可能?为什么Spring-Data中没有内置这么重要的功能?

如果我手动实现此功能:

public class UserRepositoryImpl implements UserRepository
Run Code Online (Sandbox Code Playgroud)

我必须实现大量的预定义方法 …

java spring jpa spring-data spring-data-jpa

110
推荐指数
8
解决办法
15万
查看次数

Spring boot - 不是托管类型

我使用Spring boot + JPA并在启动服务时遇到问题.

Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.nervytech.dialer.domain.PhoneSettings
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:219)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:89)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:177)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
Run Code Online (Sandbox Code Playgroud)

这是Application.java文件,

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@SpringBootApplication
public class DialerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DialerApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用UCp进行连接池,DataSource配置如下,

@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableJpaRepositories(entityManagerFactoryRef = "dialerEntityManagerFactory", transactionManagerRef = "dialerTransactionManager", basePackages = { "com.nervy.dialer.spring.jpa.repository" })
public class ApplicationDataSource { …
Run Code Online (Sandbox Code Playgroud)

spring jpa spring-mvc spring-data spring-boot

100
推荐指数
14
解决办法
19万
查看次数

Spring DAO vs Spring ORM vs Spring JDBC

我正在浏览Spring支持的数据访问技术,我注意到它提到了多个选项,我不确定它们之间的区别:

据我所知,Spring JDBC提供了模板,用于减少用于通过简单的旧方式访问数据库的样板代码 - 您编写自己的SQL查询.

Spring-ORM提供了通过ORM技术访问数据库的简化模板,如Hibernate,My(i)Batis等.

Spring-DAO按照Spring的网站:

Spring中的数据访问对象(DAO)支持旨在使您能够以一致的方式轻松使用JDBC,Hibernate或JDO等数据访问技术

我对ORM与JDBC有一点关系,因为它们针对的是访问数据库的不同方式.但Spring-DAO简直令人困惑!

有谁能请澄清这三者之间究竟有什么不同?哪种情况应该首选?

此外,还有另一个项目Spring-DATA可用(http://projects.spring.io/spring-data/)现在,它是否是Spring支持的所有数据访问技术的父项目,或者它只是Spring的新名称-dao?

spring spring-jdbc spring-orm spring-data

96
推荐指数
1
解决办法
7万
查看次数

在Spring Boot中禁用所有与数据库相关的自动配置

我使用Spring Boot开发两个应用程序,一个用作服务器,另一个用作客户端应用程序.但是,它们都是相同的应用程序,根据活动配置文件的功能不同.我正在使用Spring Boot的自动配置功能来配置我的应用程序.

我想在客户端应用程序上禁用所有与数据库相关的自动配置,因为它不需要数据库连接.应用程序不应尝试与数据库建立连接,也不应尝试使用任何Spring Data或Hibernate功能.启用或禁用数据库自​​动配置应该是有条件的,并且基于应用程序的活动配置文件.

我可以通过为各个配置文件创建两个不同的application.properties文件来实现此目的吗?

我尝试将其添加到我的属性文件中,

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration\
  org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration\
  org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration
Run Code Online (Sandbox Code Playgroud)

但是,应用程序仍然尝试在启动时连接到数据库.这些排除是否足以满足我的要求?

spring hibernate spring-data spring-data-jpa spring-boot

95
推荐指数
7
解决办法
11万
查看次数

Spring Data:支持"删除"吗?

我使用Spring JPA进行数据库访问.我能够找到像findByName和countByName这样的例子,我没有必要编写任何方法实现.我希望找到基于某些条件删除一组记录的示例.

Spring JPA是否支持deleteByName-like delete?任何指针都很受欢迎.

问候和感谢.

spring spring-data spring-data-jpa

93
推荐指数
4
解决办法
14万
查看次数

Spring Data存储库是如何实际实现的?

我已经在我的项目中使用Spring Data JPA存储库一段时间了,我知道以下几点:

  • 在存储库接口中,我们可以添加类似的方法findByCustomerNameAndPhone()(假设customerNamephone是域对象中的字段).
  • 然后,Spring通过在运行时(在应用程序运行期间)实现上述存储库接口方法来提供实现.

我感兴趣的是如何对它进行编码,我已经查看了Spring JPA源代码和API,但我找不到以下问题的答案:

  1. 如何在运行时和方法中生成并注入存储库实现类?
  2. Spring Data JPA是否使用CGlib或任何字节码操作库来实现方法并动态注入?

您能否帮助解决上述问题并提供任何支持的文档?

java spring ddd-repositories repository-pattern spring-data

93
推荐指数
1
解决办法
2万
查看次数

Spring Data JPA通过嵌入对象属性查找

我想编写一个Spring Data JPA存储库接口方法签名,它可以让我找到在该实体中具有嵌入对象属性的实体.有谁知道这是否可能,如果是这样的话怎么样?

这是我的代码:

@Entity
@Table(name = "BOOK_UPDATE_QUEUE", indexes = { uniqueConstraints = @UniqueConstraint(columnNames = {
        "bookId", "region" }, name = "UK01_BOOK_UPDATE_QUEUE"))
public class QueuedBook implements Serializable {

    @Embedded
    @NotNull
    private BookId bookId;

    ...

}

@Embeddable
public class BookId implements Serializable {

    @NotNull
    @Size(min=1, max=40)
    private String bookId;

    @NotNull
    @Enumerated(EnumType.STRING)
    private Region region;

    ...

}

public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> {

    //I'd like to write a method like this, but can't figure out how to search by region,
    //when …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data spring-data-jpa

91
推荐指数
3
解决办法
9万
查看次数

缺少CrudRepository#findOne方法

我在我的项目中使用Spring 5.直到今天还有可用的方法CrudRepository#findOne.

但下载最新快照后,它突然消失了!有没有提到该方法现在不可用?

我的依赖列表:

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}    

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-validation'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtime 'org.springframework.boot:spring-boot-devtools'

    runtime 'com.h2database:h2:1.4.194'
    compile 'org.projectlombok:lombok:1.16.14'
    compile 'org.modelmapper:modelmapper:0.7.5'


    testCompile 'org.springframework.boot:spring-boot-starter-test'

    testCompile 'org.codehaus.groovy:groovy-all:2.4.10'

    testCompile 'cglib:cglib:3.2.5'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}
Run Code Online (Sandbox Code Playgroud)

更新:

似乎这个方法已被替换 CrudRepository#findById

java spring spring-data spring-data-jpa spring-boot

89
推荐指数
4
解决办法
4万
查看次数

Spring Boot中的spring.jpa.open-in-view = true属性是什么?

我在Spring Boot文档中看到了JPA配置的这个属性spring.jpa.open-in-view=true.那么它被设置为true,它是默认值吗?

它到底意味着什么?我没有找到任何解释这种行为.

它应该是使用Hibernate SessionFactory来代替EntityManagerFactory?如果是,我该如何设置EntityManagerFactory

感谢任何帮助.

谢谢!

java spring jpa spring-data spring-boot

88
推荐指数
3
解决办法
6万
查看次数