Tag: JPA

Spring Data JPA:处理其他实体的继承属性

情况:(班级为实体) class1具有class2类型的属性 subclass1 (从class1继承)使用类型subclass2 (从class2继承)的属性 subclass2有一个class2没有的property2 我怎么能实现这个在subclass1Repository中: findByProperty_property2(xx) 很明显,我得到“无属性”属性2“找到类型class1 ,遍历路径: class2 .property2。 JPA显然不知道属性的类型是subclass2 。 @Entity open class Class1 constructor( @ManyToOne property: Class2 ) @Entity open class Class 2 constructor() { } @Entity open class subclass1 constructor( property: Subclass2 ):Class1(property = property) @Entity open class subclass2 constructor( property2: Double ):Class2(..) interface subclass1Repository: JpaRepository<Subclass1, Long>{ fun findByProperty_property2(prop2:Double): […]

Hibernate保存具有空的父ID的子实体

Hibernate不想保存子实体的ID。 我有以下表格: @Entity @Table(name = "ct_orders") data class Order( @Id @Column(name = "id") @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY) val id: Int = 0, @OneToMany(fetch = FetchType.LAZY, cascade = arrayOf(CascadeType.ALL), mappedBy = "order") val route: List<Route>? = null, … ) @Entity @Table(name = "ct_routes") @JsonIgnoreProperties("id", "order") data class Route( @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) val id: […]

通过外部配置通知命名策略

注意:这是一个Spring / Hibernate的问题。 我碰巧使用Kotlin,但也欢迎Java的答案。 JPA实体必须有一个常量表和目录名称: User.kt : @Entity @Table(name="users", catalog="auth") data class User( @Id val username: String, var password: String ) 这对我来说是个坏消息。 我的MySQL数据库托管同一个应用程序堆栈的几个不同的实例。 结果是:我有各种各样的模式,名字由环境隔开: SHOW DATABASES; sandbox_billing sandbox_auth staging_billing staging_auth preproduction_billing preproduction_auth 每个应用程序上下文都有一个数据源,可以访问两个目录: *_billing (用于域实体)和*_auth (用于身份验证信息)。 我的应用程序有一个可配置的映射com.stackoverflow.persistence.catalog-overrides ,其中可以指定用于该特定应用程序实例的目录名称: application.yml : spring: datasource: url: 'jdbc:mysql://mycooldatabase.com:3306' com.stackoverflow: persistence: catalog-overrides: billing: sandbox_billing auth: sandbox_auth 这个配置被填充到POKO中: PersistenceProperties.kt : @Component […]

Kotlinic模式使用Spring Data JPA的“查询范例”

Spring Data JPA引入了一个很好的功能,即“通过示例查询”(QBE) 。 您通过构建实体的实例来表达您的搜索条件。 您不必编写JPQL。 它比存储库查询派生使用更少的“魔术”。 语法很好。 它可以防止琐碎的存储库代码爆炸。 它很好地重构了重构。 但有一个问题:只有部分构建对象时,QBE才能正常工作。 这是我的实体: @Entity @Table(name="product") data class Product( @Id val id: String, val city: String, val shopName: String, val productName: String, val productVersion: Short ) 这里是我的存储库(空的!这是关于QBE的一件好事): @Repository interface ProductRepository : JpaRepository<Product, String> 以下是您如何获取List<Product> – 某些商店中某些城市销售的所有产品: productRepository.findAll(Example.of(Product(city = "London", shopName="OkayTea"))) 或者至少,这就是我想要做的。 有一个问题。 这个对象是不可能的 : Product(city = "London", […]

JPA为卡桑德拉?

思考开始在一个新的基于Java的项目中使用Cassandra,我发现这篇文章开始卡桑德拉 – 使用昆德拉? 别的东西? 其中说“假装卡桑德拉是一个关系数据库,昆德拉的做法是一个很好的方法,把你自己画到一个角落,而不是很了解你到达那里。” 我知道数据建模应该遵循不同的规则,但是假设我遵循这些规则,如果我用JPA层包装了Cassandra(如Kundera,DataNucleus或其他),还有什么可以丢失的呢?

指定索引(非唯一键)使用JPA

如何定义一个字段,例如使用JPA注释将email定义为索引。 我们在email上需要一个非唯一的密钥,因为每天在这个字段上有几百万个查询,而且没有密钥有点慢。 @Entity @Table(name="person", uniqueConstraints=@UniqueConstraint(columnNames={"code", "uid"})) public class Person { // Unique on code and uid public String code; public String uid; public String username; public String name; public String email; } 我已经看到一个冬眠特定的注释,但我试图避免供应商的具体解决方案,因为我们仍然决定休眠和数据核心之间。 更新: 从JPA 2.1开始,你可以做到这一点。 请参阅: 此位置不允许使用注释@Index

遇到多个对数据库序列的引用

构建JHipster应用程序时出现此错误: org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义名称为'entityManagerFactory'的bean时出错:init方法的调用失败; 嵌套的异常是org.hibernate.HibernateException:遇到数据库序列[hibernate_sequence]的多个引用尝试toset冲突值为'增量大小'。 发现[1]和[50] 我在这里看到50,但是我不知道1是从哪里来的。 自从最后一次工作以来,我在这些 Kotlin文件中添加了一些新的实体。 我正在将Thinkter管道演示作为一项功能添加到我的应用程序中。 自行演示构建和运行没有任何问题。 为了得到我现在所在的位置,我将演示中的代码复制到了我的应用程序中,并调整了一些与用户实体相关的内容。 由于JHipster不允许您向用户添加字段,因此我创建了一个与其1:1关系的Author实体。 这本身可能与这个错误没有任何关系。 我认为这个新代码中的某些东西必须尝试创建一个序列,并且默认的增量大小是1。

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 = […]

Jpa Specification来查找字段值的子集

我正在持久层上使用Spring Data JPA编写JpaSpecificationExecutor应用程序,更具体地说,我的DAO扩展了JpaSpecificationExecutor接口,所以我能够实现某种过滤器; 想象具有多个属性的Item列表(为了清楚起见,我省略了注释和其他元数据): data class Item(var tags: MutableList<String>) 在我的服务层上,我的过滤方法如下所示: fun findBy(tagsToFilterBy: List<String>): List<Items> { return dao.findAll { root, query, builder -> builder.//?? } } 我想要实现的是仅检索包含完全tagsToFilterBy ,换句话说, tagsToFilterBy应该是tagsToFilterBy一个子集。 我知道isMember(…)方法,但我认为它的使用不会很令人愉快,因为它只接受一个“实体”在调用。 你可以给我一些建议吗? 我的另一个问题是,是否可以直接使用用户输入,比如builder.like(someExpression, inputFromUser)或者我必须把它放在builder.parameter(…)然后query.setParameter(…) 。 谢谢你的任何想法

即使使用noarg插件,也没有使用Kotlin的JPA实体的默认构造函数

我开始学习Kotlin,目前版本为1.1.0。 首先,我正在重写我的实体和当前项目中的测试。 我已经读过,你需要实体类的“kotlin-maven-noarg”,以便它生成默认参数少的构造函数,但是我的项目仍然抱怨。 你能告诉我我做错了什么吗? pom.xml中: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> {…} <build> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <testSourceDirectory>src/test/java</testSourceDirectory> <testResources> <testResource> <directory>src/test/resources</directory> </testResource> </testResources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> […]