标签: spring-data-jpa

使用Spring CrudRepository进行不区分大小写的查询

使用Spring CrudRepository Query; 我想用"name"属性选择"DeviceType"实体.但是以下查询以区分大小写的方式选择权限.我如何使它不区分大小写.谢谢.

public interface DeviceTypeRepository extends CrudRepository<DeviceType, Integer>, JpaSpecificationExecutor<DeviceType> {

    public Iterable<DeviceType> findByNameContaining(String name);

}  
Run Code Online (Sandbox Code Playgroud)

spring spring-data-jpa

90
推荐指数
2
解决办法
8万
查看次数

%Like Spring JpaRepository中的%Query

我想写一个类似的查询,JpaRepository但它没有返回任何东西:

LIKE '%place%'- 没有用.

LIKE 'place' 工作得很好.

这是我的代码:

@Repository("registerUserRepository")
public interface RegisterUserRepository extends 
JpaRepository<Registration,Long>{

    @Query("Select c from Registration c where c.place like:place")
     List<Registration> findByPlaceContaining(@Param("place")String place);
}
Run Code Online (Sandbox Code Playgroud)

java jpa spring-data-jpa

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

缺少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万
查看次数

FetchMode如何在Spring Data JPA中工作

我确实在我的项目中有三个模型对象之间的关系(帖子末尾的模型和存储库片段).

当我打电话时PlaceRepository.findById它会触发三个选择查询:

( "SQL")

  1. SELECT * FROM place p where id = arg
  2. SELECT * FROM user u where u.id = place.user.id
  3. SELECT * FROM city c LEFT OUTER JOIN state s on c.woj_id = s.id where c.id = place.city.id

这是相当不寻常的行为(对我而言).在阅读Hibernate文档后我可以告诉它应该总是使用JOIN查询.在类中FetchType.LAZY更改为 查询(使用附加SELECT 查询)时,查询没有区别,更改为 (使用JOIN查询)时类相同.FetchType.EAGERPlaceCityFetchType.LAZYFetchType.EAGER

当我使用CityRepository.findById抑制火时两个选择:

  1. SELECT * FROM city c where id = arg
  2. SELECT * FROM state s where id = city.state.id

我的目标是在所有情况下都有一个sam行为(总是JOIN或SELECT,但是JOIN首选).

型号定义:

地点:

@Entity
@Table(name = …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-data-jpa

74
推荐指数
3
解决办法
8万
查看次数

如何通过Sort和Pageable使用Spring数据JPA开箱即用地查询数据?

我在我的项目中尝试Spring数据JPA.我想知道是否有出的现成的API来查询数据,双方SortPageable.当然,我知道我可以自己编写这个方法,我只是想知道是否有一个开箱即用的方法.我的DAO扩展了JpaRepository,我发现有以下方法可以调用:

findAll();
findAll(Pageable pageable);
findAll(Sort sort);
Run Code Online (Sandbox Code Playgroud)

但是没有这样的方法findAll(Sort sort, Pageable pageable),所以我很好奇.

java spring jpa spring-data-jpa

70
推荐指数
2
解决办法
10万
查看次数

如何在Spring Data中使用@Transactional?

我刚刚开始研究Spring-data,Hibernate,MySQL,JPA项目.我切换到spring-data,这样我就不用担心手工创建查询了.

我注意到@Transactional当你使用spring-data时不需要使用,因为我也在没有注释的情况下尝试了我的查询.

我应该/不应该使用@Transactional注释的具体原因是什么?

作品:

@Transactional
public List listStudentsBySchool(long id) {
    return repository.findByClasses_School_Id(id);
}
Run Code Online (Sandbox Code Playgroud)

还有效:

public List listStudentsBySchool(long id) {
    return repository.findByClasses_School_Id(id);
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

java spring jpa spring-data spring-data-jpa

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

Spring数据jpa-没有定义名为'entityManagerFactory'的bean; 注入自动连接的依赖项失败

我正在使用spring数据jpa,hibernate,mysql,tomcat7,maven开发应用程序,这就是创建错误.我想弄明白但是我失败了.

错误是在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用; 没有定义名为'entityManagerFactory'的bean; 注入自动连接的依赖项失败

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initDbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.wahid.cse.repository.RoleRepository org.wahid.cse.service.InitDbService.roleRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#c08f81' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#c08f81': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' …
Run Code Online (Sandbox Code Playgroud)

java jpa spring-mvc spring-data spring-data-jpa

62
推荐指数
5
解决办法
10万
查看次数

无法在Spring Boot中自动加载@Repository注释接口

我正在开发一个spring boot应用程序,我在这里遇到了一个问题.我正在尝试注入一个@Repository注释接口,它似乎根本不起作用.我收到了这个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootRunner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at …
Run Code Online (Sandbox Code Playgroud)

spring repository spring-data spring-data-jpa spring-boot

62
推荐指数
8
解决办法
12万
查看次数

crudrepository findBy方法签名有多个in运算符?

我有一个像这样的实体类:

@Entity
@Table(name = "EMAIL")
class Email{
    @Id
    @Column(name = "Id")
    Long id;
    @Column(name = "EMAIL_ID")
    String emailId;
    @Column(name = "PIN_CODE")
    String pincode;
}
Run Code Online (Sandbox Code Playgroud)

如何findBy使用crudrepository spring data jpa为下面的查询编写方法?

select email_id,name from email_details where eamil_id in('mike@gmail.com','ram@gmail.com') and pin_code in('633677','733877')
Run Code Online (Sandbox Code Playgroud)

我期待像下面的spring数据jpa方法但是如何构造它?

List<Email> findBy.....(List<String> emails, List<String> pinCodes);
Run Code Online (Sandbox Code Playgroud)

我想在单个数据库命中中获取电子邮件列表.

java spring spring-data spring-data-jpa

62
推荐指数
1
解决办法
9万
查看次数

Lombok @Builder和JPA Default构造函数

我正在使用项目Lombok和Spring Data JPA.有没有办法将Lombok @Builder与JPA默认构造函数连接?

码:

@Entity 
@Builder
class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
}
Run Code Online (Sandbox Code Playgroud)

据我所知,JPA需要默认构造函数,它被@Builder注释覆盖.那有什么解决方法吗?

这段代码给了我错误: org.hibernate.InstantiationException: No default constructor for entity: : app.domain.model.Person

java spring lombok spring-data-jpa

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