Tag: spring data jpa

DataJPATest Junit方法命名和NullPointerExceptions

我正在使用SpringBoot / Kotlin / JPA / Hibernate / Junit,并且拥有JpaServiceTest类,可以执行与单个实体有关的存储库方法。 JpaService类的方法名遵循约定findByXXXXId , findAll , addXXXX , addXXXX和deleteXXXX 。 为了保持一致性,我使用相同的约定为JpaTest类中的方法命名。 我的JpaTest类有两个findById场景,其中’Null’预期是另一个返回映射实体的地方。 我的应用程序按预期方式工作,但是我的测试类在findById方案上失败,这个方案有望返回一个有效的实体。 The service class @Service(“MyService”) @Transactional internal class JpaMyService(val MyRepo: MyRepository) : MyService { val log = LoggerFactory.getLogger(“MyService”) override fun findByMyId(MyId: Long): MyDto? { log.debug(“Retrieving My: {}”, MyId) return MyRepo.findOne(MyId)?.toDto() } override fun findAllMys(): List { […]

在使用自定义的AttributeConverter for Neo4j时获取Neo.ClientError.Statement.TypeError Spring Boot JPA

我有一个与Spring Data Neo4j(v5.0.0RC2),Neo4j(v3.2.1)和Neo4j OGM(v3.0.0)一起运行的Kotlin(v1.1.4)Spring Boot(v2.0.0.BUILD-SNAPSHOT)应用程序。 尝试对具有自定义AttributeConverter的字段使用自定义JPA @Query时收到以下错误。 堆栈跟踪: org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing Cypher; Code: Neo.ClientError.Statement.TypeError; Description: Property values can only be of primitive types or arrays thereof; nested exception is org.neo4j.ogm.exception.CypherException: Error executing Cypher; Code: Neo.ClientError.Statement.TypeError; Description: Property values can only be of primitive types or arrays thereof at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at […]

Spring JPA查询总是使用序列扫描而不是索引扫描

我有一个简单的查询 @Query(value = “select * from some_table where consumer_id=:consumerId and store_id=:storeId and cancelled_at is null”, nativeQuery = true) fun checkIfNewConsumer(consumerId: BigInteger, storeId: BigInteger): List 当我运行查询时,直接对超过3000万行的表进行解释 Index Scan using select_index on some_table (cost=0.56..8.59 rows=1 width=86) (actual time=0.015..0.015 rows=0 loops=1) Index Cond: ((consumer_id = 1234) AND (store_id = 4) AND (cancelled_at IS NULL)) Planning time: 0.130 ms […]

hibernate没有正确构建查询

我的项目是用Spring Boot和Kotlin构建的。 我有一个Message ,可能有一个父消息(当回应消息),代码如下所示: @Entity class Message( @field: Id @field:GeneratedValue var id : Long = 0, @field: ManyToOne(targetEntity = Employee::class) var sender:Employee?=null, @field: ManyToOne(targetEntity = Employee::class) var receiver:Employee?=null, var time:LocalDateTime, var content: String, var read:Boolean, @field: ManyToOne(targetEntity = Message::class) @Nullable var father:Message?=null) {} 当调用这个查询(使用JpaRepository)时: fun findByReceiverIdAndReadFalse(id:Long):List 我得到这个错误: Unknown column ‘message0_.father_id’ in ‘field list’ 它实际上应该是message0_.father.id ,而不是father_id […]

坚持与Kotlin和SpringBoot LocalDate

我正在用SpringBoot和JPA尝试Kotlin。 我试图坚持一个LocalDate但有一个错误。 以下是我的实体的cody: @Entity @Table(name = “season”) data class Season(val name: String, @Convert(converter = LocalDateAttributeConverter::class) val from: LocalDate, @Convert(converter = LocalDateAttributeConverter::class) val to: LocalDate, @Enumerated(EnumType.STRING) val status: Status, @Id @GeneratedValue val id: Int = -1) enum class Status { CURRENT, CLOSED } 转换器: @Converter(autoApply = true) class LocalDateAttributeConverter : AttributeConverter { override fun convertToDatabaseColumn(locDate: LocalDate?): […]

Kotlin的DAO应该返回Optional还是null?

在Kotlin / JPA之前,我曾经写过这样的DAO层: public interface UserDao extends JpaRepository { Optional findBySsn(String ssn); } 而在来电方,如果我想通过SSN找人或创建用户,我可以这样写: val user = userDao.findBySsn(value).orElseGet { userDao.save(value) } 它运作良好,看起来流利。 但是,由于Kotlin引入了无效安全,所以还有另外一种惯用的方式(Java中的dao): public interface UserDao extends JpaRepository { Optional findBySsn(String ssn); @Query(“select u from User u where u.ssn = :ssn”) @Nullable User findBySsnNullable(@Param(“ssn”) String ssn) } 而在客户端: val user = userDao.findBySsnNullable(value) .takeIf{ it -> […]

Kotlin和Spring Data JPA产生PropertyReferenceException

我目前正在使用Spring Data JPA的Kotlin 1.2和Spring Boot 2.0 M7。 在这个项目中,我正在使用一个自定义的基础知识库,而不是JPARepository或PagingAndSortingRepository(实际上并不重要) 这里是基础接口 @NoRepositoryBean interface BaseRepository : Repository { fun save(entity: S): S fun findOne(id: ID): T? fun findAll(): List fun count(): Long } 这里是实际的存储库 interface ArticleRepository : BaseRepository { } 最后这里是文章数据类 @Entity @Table(name = “article”) @Cacheable data class Article ( @Id @GeneratedValue(strategy = IDENTITY) @Column(name = “id”, unique […]

@CreationTimestamp和@UpdateTimestamp在Kotlin中不起作用

这是我的标记和发布实体类: @Entity class Tag( @get:NotBlank @Column(unique = true) val name: String = “”, val description: String = “” ) { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) val id: Int? = null @ManyToMany(mappedBy = “tags”) val posts: MutableSet = mutableSetOf() @CreationTimestamp lateinit var createDate: Date @UpdateTimestamp lateinit var updateDate: Date fun addPost(post: Post) { this.posts.add(post) post.tags.add(this) } } […]

@Param不能在Spring Data JPA中工作

我正在建立一个Spring Data JPA Repo来处理postgresql数据库中的序列。 我假设这将是非常简单的: @Query(nativeQuery = true, value = "CREATE SEQUENCE IF NOT EXISTS ':seq_name' START WITH :startAt") fun createSequence(@Param("seq_name") seq_name: String, @Param("startAt") startAt: Long = 0) @Query(nativeQuery = true, value = "SELECT nextval(':seq_name')") fun nextSerial(@Param("seq_name") seq_name: String) : Long @Query(nativeQuery = true, value = "DROP SEQUENCE IF EXISTS ':seq_name'") fun dropSequence(@Param("seq_name") seq_name: String) […]

Spring数据JPA中的动态查询

我正在寻找一个解决方案来动态构建使用Spring Data JPA的查询。 我有一个GameController,它有一个REST风格的服务端点/游戏,它需要4个可选参数:流派,平台,年份,标题。 这些API可能不会被传递,全部4个,以及它们之间的每个组合。 如果有任何参数未被传递,则默认为null。 我需要一个在仓库中的方法,将建立适当的查询,理想情况下,仍然允许Spring Data JPA分页,但我不知道这是可能的。 我发现这篇文章,但这似乎并不是我所需要的,除非我是误解。 http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ 我知道JPA有一个查询标准的API,但真的不知道如何实现这一点。 我意识到我可以为每个可能的场景创建一个方法,但这似乎是非常糟糕的做法和大量不必要的代码。 GameRepository: package net.jkratz.igdb.repository; import net.jkratz.igdb.model.Game; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface GameRepository extends JpaRepository<Game, Long> { @Query("select g from Game g, GamePlatformMap gpm, Platform p where g = gpm.game and gpm.platform = p and p.id = […]