标签: spring-data

存在Spring数据CrudRepository

当我扩展CrudRepository接口时,我的子接口中有exists(ID)方法.我可以写findBy<property>方法.

有可能以某种方式编写existBy<property>将返回的方法boolean.或者用@Query(jpa query)它来注释它将返回boolean.

我知道我可以做select count(*)并返回long,但是我必须!=0检查我的服务层.

spring spring-data spring-data-jpa

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

如何在spring-boot中禁用spring-data-mongodb自动配置

有没有人尝试在spring-boot中禁用mongodb的自动配置?

我正在尝试使用spring-data-mongodb进行spring-boot; 使用基于java的配置; 使用spring-boot 1.2.1.RELEASE,我导入spring-boot-starter-web及其父pom进行依赖管理.我还导入了spring-data-mongodb(尝试过spring-boot-starter-mongodb).

我需要连接到两个不同的MongoDB服务器.所以我需要为mongo连接,MongoTemplate等配置两组实例.我还想禁用自动配置.由于我连接到多个服务器,因此我不需要自动配置单个默认的MongoTemplate和GridFsTemplate bean.

我的主要课程如下:

@Configuration
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan
//@SpringBootApplication // @Configuration @EnableAutoConfiguration @ComponentScan 
public class MainRunner {

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

我的两个mongo配置类看起来像这样:

@Configuration
@EnableMongoRepositories(basePackageClasses = {Test1Repository.class},
        mongoTemplateRef = "template1",
        includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test1Repository")}
)
public class Mongo1Config {

    @Bean
    public Mongo mongo1() throws UnknownHostException {
        return new Mongo("localhost", 27017);
    }

    @Primary
    @Bean
    public MongoDbFactory mongoDbFactory1() throws UnknownHostException {
        return new SimpleMongoDbFactory(mongo1(), "test1");
    }

    @Primary
    @Bean …
Run Code Online (Sandbox Code Playgroud)

java spring-data spring-data-mongodb spring-boot

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

具有分页的Spring-Data FETCH JOIN不起作用

我正在尝试使用HQL使用JOIN FETCH获取我的实体以及子实体,如果我想要所有结果,这是正常工作但如果我想要一个页面则不是这样

我的实体是

@Entity
@Data
public class VisitEntity {

    @Id
    @Audited
    private long id;

    .
    .
    .   

    @OneToMany(cascade = CascadeType.ALL,)
    private List<VisitCommentEntity> comments;
}
Run Code Online (Sandbox Code Playgroud)

因为我有数百万次访问,我需要使用Pageable,我想在单个数据库查询中获取注释,如:

@Query("SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and ..." )
public Page<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...,
        Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

该HQL调用抛出以下异常:

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=com.ro.lib.visit.entity.VisitEntity.comments,tableName=visitdb.visit_comment,tableAlias=comments1_,origin=visitdb.visit visitentit0_,columns={visitentit0_.visit_id ,className=com.ro.lib.visit.entity.VisitCommentEntity}}] …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data spring-data-jpa

27
推荐指数
3
解决办法
1万
查看次数

获取有关Spring Data数据的最后记录

我正在尝试在Spring Data存储库中定义一个方法来获取按日期排序的表上的最后记录.这是我的实体:

@Entity
public class News {

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String title;

    @Column(nullable = false)
    private String text;

    private Date publicationDate;

    /* Getters and Setters */
}
Run Code Online (Sandbox Code Playgroud)

这是我的存储库:

public interface NewsRepository extends JpaRepository<News, Long> {
    List<News> findFirst5OrderByPublicationDateDesc();
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用启动项目,我会收到下一个错误:

引起:org.springframework.data.mapping.PropertyReferenceException:找不到类型Date的属性desc!遍历路径:News.publicationDate.

如果我删除了Desc,我会得到这个:

引起:java.util.NoSuchElementException

我做错了什么?

java spring spring-mvc spring-data

27
推荐指数
2
解决办法
3万
查看次数

使用spring数据jpa更新单个字段

我正在使用spring-data的存储库 - 非常方便,但我遇到了一个问题.我可以轻松更新整个实体,但我相信当我只需要更新一个字段时,这是毫无意义的:

@Entity
@Table(schema = "processors", name = "ear_attachment")
public class EARAttachment {

    private Long id;
    private String originalName;
    private String uniqueName;//yyyy-mm-dd-GUID-originalName
    private long size;
    private EARAttachmentStatus status;
Run Code Online (Sandbox Code Playgroud)

更新我只是调用方法保存.在日志中我看到了跟随:

batching 1 statements: 1: update processors.ear_attachment set message_id=100, 
original_name='40022530424.dat', 
size=506, 
status=2,
unique_name='2014-12-16-8cf74a74-e7f3-40d8-a1fb-393c2a806847-40022530424.dat'
where id=1 
Run Code Online (Sandbox Code Playgroud)

我想看到这样的事情:

batching 1 statements: 1: update processors.ear_attachment set status=2 where id=1 
Run Code Online (Sandbox Code Playgroud)

Spring的存储库有很多工具可以使用名称约定来选择一些东西,也许像updateForStatus(int status)这样的更新有类似的东西;

java spring updates spring-data spring-data-jpa

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

Spring Data JPA NamedStoredProcedureQuery多个输出参数

我有一个简单的存储过程,我用它来测试Spring Data JPA存储过程功能.

create or replace procedure plus1inout (arg in int,res1 out int,res2 out int) is
BEGIN   
 res1 := arg + 1; 
 res2 := res1 + 1;
END;
Run Code Online (Sandbox Code Playgroud)

我的代码是:

@Repository
public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
    @Procedure(name = "plus1")
    Object[] plus1(@Param("arg") Integer arg);
}

@Entity
@NamedStoredProcedureQuery(name = "plus1", procedureName = "ADJUD.PLUS1INOUT",
        parameters = {
        @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res1", type = Integer.class),
        @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type …
Run Code Online (Sandbox Code Playgroud)

java stored-procedures hibernate spring-data spring-data-jpa

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

如何保持与Spring Data REST和JPA的双向关系?

使用Spring Data REST,如果您有一个OneToMany或一个ManyToOne关系,PUT操作会在"非拥有"实体上返回200,但实际上并不会保留已连接的资源.

示例实体:

@Entity(name = 'author')
@ToString
class AuthorEntity implements Author {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id

    String fullName

    @ManyToMany(mappedBy = 'authors')
    Set<BookEntity> books
}


@Entity(name = 'book')
@EqualsAndHashCode
class BookEntity implements Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id

    @Column(nullable = false)
    String title

    @Column(nullable = false)
    String isbn

    @Column(nullable = false)
    String publisher

    @ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
    Set<AuthorEntity> authors
}
Run Code Online (Sandbox Code Playgroud)

如果您使用a来支持它们PagingAndSortingRepository,您可以获取a Book,按照authors书上的链接进行PUT,并使用要关联的作者的URI.你不能走另一条路.

如果您对作者执行GET并在其books链接上执行PUT …

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

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

Spring Data REST的QueryDSL集成可用于执行更复杂的查询吗?

我目前正在构建一个REST API,我希望客户端可以轻松地过滤特定实体的大多数属性.使用QueryDSL与结合春季数据REST(由奥利弗·基尔克一个例子),让我很容易地通过允许客户通过组合是指性质(如查询参数进行过滤得到我想要的东西90% /users?firstName=Dennis&lastName=Laumen).

我甚至可以通过实现QuerydslBinderCustomizer接口来自定义查询参数和实体属性之间的映射(例如,用于不区分大小写的搜索或部分字符串匹配).这一切都很棒,但我也希望客户能够使用范围过滤某些类型.例如关于像出生日期这样的财产,我想做类似下面的事情,/users?dateOfBirthFrom=1981-1-1&dateOfBirthTo=1981-12-31.基于数字的属性也是如此/users?idFrom=100&idTo=200.我觉得这应该可以使用QuerydslBinderCustomizer界面,但这两个库之间的集成没有得到非常广泛的记录.

总结一下,这可能使用Spring Data REST和QueryDSL吗?如果是这样,怎么样?

spring querydsl spring-data spring-data-jpa spring-data-rest

27
推荐指数
3
解决办法
8883
查看次数

Spring数据jpa - 如何通过方法名称组合多个And和Or

我正在尝试迁移该应用程序.我正在从Hibernate工作到Spring Data Jpa.

虽然spring数据jpa提供了简单的查询构建方法,但我仍然坚持创建使用And和的查询方法Or operator.

MethodName - findByPlan_PlanTypeInAndSetupStepIsNullOrStepupStepIs(...)

当它转换为查询时,前两个表达式被组合并执行为[(exp1 and exp2) or (exp3)].

而要求是](exp1) and (exp2 or exp3)].

任何人都可以告诉我,如果这是可以实现的 Spring data jpa?

java spring-data spring-data-jpa

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

如何直接从Java中的mongodb查询返回原始JSON?

我有以下代码:

@RequestMapping(value = "/envinfo", method = RequestMethod.GET)
@ResponseBody
public Map getEnvInfo()
{
    BasicQuery basicQuery = new BasicQuery("{_id:'51a29f6413dc992c24e0283e'}", "{'envinfo':1, '_id': false }");
    Map envinfo= mongoTemplate.findOne(basicQuery, Map.class, "jvmInfo");
    return envinfo;
}
Run Code Online (Sandbox Code Playgroud)

你可以注意到,代码:

  1. 从MongoDB中检索JSON
  2. 将其转换为Map对象
  3. Map然后,Spring MongoData 将对象转换为JSON,然后返回到浏览器.

是否可以直接从MongoDb返回原始json而无需经过中间转换步骤?

java spring mongodb spring-data spring-data-mongodb

26
推荐指数
2
解决办法
3万
查看次数